[bcl] Remove ONLY_1_1 defines from class libs
[mono-project.git] / mcs / class / System.Web.Services / System.Web.Services.Configuration / WebServicesConfigurationSectionHandler.cs
blobc1cc2629eda2bc49e0aa53194f2d519b15424f6a
1 //
2 // System.Web.Services.Configuration.WebServicesConfigurationSectionHandler
3 //
4 // Authors:
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
8 //
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:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
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;
32 using System.Collections;
33 using System.Configuration;
34 using System.Xml;
36 namespace System.Web.Services.Configuration
38 [Flags]
39 enum WSProtocol
41 HttpSoap = 1,
42 HttpPost = 1 << 1,
43 HttpGet = 1 << 2,
44 Documentation = 1 << 3,
45 HttpSoap12 = 1 << 4,
46 HttpPostLocalhost = 1 << 5,
47 AnyHttpSoap = HttpSoap | HttpSoap12,
48 All = 0xFF
51 class WSConfig
53 volatile static WSConfig instance;
54 WSProtocol protocols;
55 string wsdlHelpPage;
56 string filePath;
57 ArrayList extensionTypes = new ArrayList();
58 ArrayList extensionImporterTypes = new ArrayList();
59 ArrayList extensionReflectorTypes = new ArrayList();
60 ArrayList formatExtensionTypes = new ArrayList();
61 static readonly object lockobj = new object ();
63 public WSConfig (WSConfig parent, object context)
65 if (parent == null)
66 return;
68 protocols = parent.protocols;
69 wsdlHelpPage = parent.wsdlHelpPage;
70 if (wsdlHelpPage != null)
71 filePath = parent.filePath;
72 else
73 filePath = context as string;
76 static WSProtocol ParseProtocol (string protoName, out string error)
78 WSProtocol proto;
79 error = null;
81 try {
82 proto = (WSProtocol) Enum.Parse (typeof (WSProtocol), protoName);
83 } catch {
84 error = "Invalid protocol name";
85 return 0;
88 return proto;
91 // Methods to modify configuration values
92 public bool AddProtocol (string protoName, out string error)
94 if (protoName == "All") {
95 error = "Invalid protocol name";
96 return false;
99 WSProtocol proto = ParseProtocol (protoName, out error);
100 if (error != null)
101 return false;
103 protocols |= proto;
104 return true;
107 public bool RemoveProtocol (string protoName, out string error)
109 if (protoName == "All") {
110 error = "Invalid protocol name";
111 return false;
114 WSProtocol proto = ParseProtocol (protoName, out error);
115 if (error != null)
116 return false;
118 protocols &= ~proto;
119 return true;
122 public void ClearProtocol ()
124 protocols = 0;
127 // Methods to query/get configuration
128 public static bool IsSupported (WSProtocol proto)
130 return ((Instance.protocols & proto) == proto && (proto != 0) && (proto != WSProtocol.All));
133 // Properties
134 public string WsdlHelpPage {
135 get { return wsdlHelpPage; }
136 set { wsdlHelpPage = value; }
139 public string ConfigFilePath {
140 get { return filePath; }
141 set { filePath = value; }
144 static public WSConfig Instance {
145 get {
146 //TODO: use HttpContext to get the configuration
147 if (instance != null)
148 return instance;
150 lock (lockobj) {
151 if (instance != null)
152 return instance;
154 instance = (WSConfig) ConfigurationSettings.GetConfig ("system.web/webServices");
157 return instance;
161 public ArrayList ExtensionTypes {
162 get { return extensionTypes; }
165 public ArrayList ExtensionImporterTypes {
166 get { return extensionImporterTypes; }
169 public ArrayList ExtensionReflectorTypes {
170 get { return extensionReflectorTypes; }
173 public ArrayList FormatExtensionTypes {
174 get { return formatExtensionTypes; }
179 enum WSExtensionGroup
181 High,
185 class WSExtensionConfig
187 Type type;
188 int priority;
189 WSExtensionGroup group;
191 public Exception SetType (string typeName)
193 Exception exc = null;
195 try {
196 type = Type.GetType (typeName, true);
197 } catch (Exception e) {
198 exc = e;
201 return exc;
204 public Exception SetPriority (string prio)
206 if (prio == null || prio == "")
207 return null;
209 Exception exc = null;
210 try {
211 priority = Int32.Parse (prio);
212 } catch (Exception e) {
213 exc = e;
216 return exc;
219 public Exception SetGroup (string grp)
221 if (grp == null || grp == "")
222 return null;
224 Exception exc = null;
225 try {
226 group = (WSExtensionGroup) Int32.Parse (grp);
227 if (group < WSExtensionGroup.High || group > WSExtensionGroup.Low)
228 throw new ArgumentOutOfRangeException ("group", "Must be 0 or 1");
229 } catch (Exception e) {
230 exc = e;
233 return exc;
236 // Getters
237 public Type Type {
238 get { return type; }
241 public int Priority {
242 get { return priority; }
245 public WSExtensionGroup Group {
246 get { return group; }
250 class WebServicesConfigurationSectionHandler : IConfigurationSectionHandler
252 public object Create (object parent, object context, XmlNode section)
254 WSConfig config = new WSConfig (parent as WSConfig, context);
256 if (section.Attributes != null && section.Attributes.Count != 0)
257 ThrowException ("Unrecognized attribute", section);
259 XmlNodeList nodes = section.ChildNodes;
260 foreach (XmlNode child in nodes) {
261 XmlNodeType ntype = child.NodeType;
262 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
263 continue;
265 if (ntype != XmlNodeType.Element)
266 ThrowException ("Only elements allowed", child);
268 string name = child.Name;
269 if (name == "protocols") {
270 ConfigProtocols (child, config);
271 continue;
274 if (name == "soapExtensionTypes") {
275 ConfigSoapExtensionTypes (child, config.ExtensionTypes);
276 continue;
279 if (name == "soapExtensionReflectorTypes") {
280 ConfigSoapExtensionTypes (child, config.ExtensionReflectorTypes);
281 continue;
284 if (name == "soapExtensionImporterTypes") {
285 ConfigSoapExtensionTypes (child, config.ExtensionImporterTypes);
286 continue;
289 if (name == "serviceDescriptionFormatExtensionTypes") {
290 ConfigFormatExtensionTypes (child, config);
291 continue;
294 if (name == "wsdlHelpGenerator") {
295 string href = AttValue ("href", child, false);
296 if (child.Attributes != null && child.Attributes.Count != 0)
297 HandlersUtil.ThrowException ("Unrecognized attribute", child);
299 config.ConfigFilePath = context as string;
300 config.WsdlHelpPage = href;
301 continue;
304 ThrowException ("Unexpected element", child);
307 return config;
310 static void ConfigProtocols (XmlNode section, WSConfig config)
312 if (section.Attributes != null && section.Attributes.Count != 0)
313 ThrowException ("Unrecognized attribute", section);
315 XmlNodeList nodes = section.ChildNodes;
316 foreach (XmlNode child in nodes) {
317 XmlNodeType ntype = child.NodeType;
318 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
319 continue;
321 if (ntype != XmlNodeType.Element)
322 ThrowException ("Only elements allowed", child);
324 string name = child.Name;
325 string error;
326 if (name == "add") {
327 string protoName = AttValue ("name", child, false);
328 if (child.Attributes != null && child.Attributes.Count != 0)
329 HandlersUtil.ThrowException ("Unrecognized attribute", child);
331 if (!config.AddProtocol (protoName, out error))
332 ThrowException (error, child);
334 continue;
337 if (name == "remove") {
338 string protoName = AttValue ("name", child, false);
339 if (child.Attributes != null && child.Attributes.Count != 0)
340 HandlersUtil.ThrowException ("Unrecognized attribute", child);
342 if (!config.RemoveProtocol (protoName, out error))
343 ThrowException (error, child);
345 continue;
348 if (name == "clear") {
349 if (child.Attributes != null && child.Attributes.Count != 0)
350 HandlersUtil.ThrowException ("Unrecognized attribute", child);
352 config.ClearProtocol ();
353 continue;
356 ThrowException ("Unexpected element", child);
360 static void ConfigSoapExtensionTypes (XmlNode section, ArrayList extensions)
362 if (section.Attributes != null && section.Attributes.Count != 0)
363 ThrowException ("Unrecognized attribute", section);
365 XmlNodeList nodes = section.ChildNodes;
366 foreach (XmlNode child in nodes) {
367 XmlNodeType ntype = child.NodeType;
368 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
369 continue;
371 if (ntype != XmlNodeType.Element)
372 ThrowException ("Only elements allowed", child);
374 string name = child.Name;
375 if (name == "add") {
376 string seType = AttValue ("type", child, false);
377 string priority = AttValue ("priority", child);
378 string group = AttValue ("group", child);
379 if (child.Attributes != null && child.Attributes.Count != 0)
380 HandlersUtil.ThrowException ("Unrecognized attribute", child);
382 WSExtensionConfig wse = new WSExtensionConfig ();
383 Exception e = wse.SetType (seType);
384 if (e != null)
385 ThrowException (e.Message, child);
387 e = wse.SetPriority (priority);
388 if (e != null)
389 ThrowException (e.Message, child);
391 e = wse.SetGroup (group);
392 if (e != null)
393 ThrowException (e.Message, child);
395 extensions.Add (wse);
396 continue;
399 ThrowException ("Unexpected element", child);
403 static void ConfigFormatExtensionTypes (XmlNode section, WSConfig config)
405 if (section.Attributes != null && section.Attributes.Count != 0)
406 ThrowException ("Unrecognized attribute", section);
408 XmlNodeList nodes = section.ChildNodes;
409 foreach (XmlNode child in nodes) {
410 XmlNodeType ntype = child.NodeType;
411 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
412 continue;
414 if (ntype != XmlNodeType.Element)
415 ThrowException ("Only elements allowed", child);
417 string name = child.Name;
418 if (name == "add") {
419 string typeName = AttValue ("name", child, false);
420 if (child.Attributes != null && child.Attributes.Count != 0)
421 HandlersUtil.ThrowException ("Unrecognized attribute", child);
423 try {
424 config.FormatExtensionTypes.Add (Type.GetType (typeName, true));
425 } catch (Exception e) {
426 ThrowException (e.Message, child);
428 continue;
431 ThrowException ("Unexpected element", child);
435 // To save some typing...
436 static string AttValue (string name, XmlNode node, bool optional)
438 return HandlersUtil.ExtractAttributeValue (name, node, optional);
441 static string AttValue (string name, XmlNode node)
443 return HandlersUtil.ExtractAttributeValue (name, node, true);
446 static void ThrowException (string message, XmlNode node)
448 HandlersUtil.ThrowException (message, node);
453 class HandlersUtil
455 private HandlersUtil ()
459 static internal string ExtractAttributeValue (string attKey, XmlNode node)
461 return ExtractAttributeValue (attKey, node, false);
464 static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional)
466 if (node.Attributes == null) {
467 if (optional)
468 return null;
470 ThrowException ("Required attribute not found: " + attKey, node);
473 XmlNode att = node.Attributes.RemoveNamedItem (attKey);
474 if (att == null) {
475 if (optional)
476 return null;
477 ThrowException ("Required attribute not found: " + attKey, node);
480 string value = att.Value;
481 if (value == String.Empty) {
482 string opt = optional ? "Optional" : "Required";
483 ThrowException (opt + " attribute is empty: " + attKey, node);
486 return value;
489 static internal void ThrowException (string msg, XmlNode node)
491 if (node != null && node.Name != String.Empty)
492 msg = msg + " (node name: " + node.Name + ") ";
493 throw new ConfigurationException (msg, node);