[Mono-list] ProviderSectionHandler revisited

Marco Canini marco.canini@fastwebnet.it
Mon, 27 Oct 2003 19:43:26 +0100


This is a multi-part message in MIME format.
--------------010606040901040709090906
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

Could someone review this new version of ProviderSectionHandler.cs and 
if right commit it please.
I need ProviderFactory to work for my job.

MC

--------------010606040901040709090906
Content-Type: text/plain;
 name="ProviderSectionHandler.cs"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="ProviderSectionHandler.cs"

//
// Mono.Data.ProviderSectionHandler
//
// Authors:
//   Brian Ritchie (brianlritchie@hotmail.com) 
//  
//
// Copyright (C) Brian Ritchie, 2002
// 
// Modified by Marco Canini <marco.canini@fastwebnet.it> on 24 October 2003
// * Use XPath to select all provider node under providers to get sure it doesn't
//   throw exception when there're comments and whitespaces in providers node

using System;
using System.Xml;
using System.Configuration;

namespace Mono.Data
{
	public class ProviderSectionHandler : IConfigurationSectionHandler
	{
		public virtual object Create(object parent,object configContext,XmlNode section)
		{
			ProviderCollection providers = new ProviderCollection ();
			
			XmlNodeList ProviderList = section.SelectNodes ("./provider");

			foreach (XmlNode ProviderNode in ProviderList) {
				Provider provider = new Provider (
					GetStringValue (ProviderNode,"name",true),
					GetStringValue (ProviderNode,"connection",true),
					GetStringValue (ProviderNode,"adapter",true),
					GetStringValue (ProviderNode,"command",true),
					GetStringValue (ProviderNode,"assembly",true),
					GetStringValue (ProviderNode,"description",false));
				providers.Add (provider);
			}
			return providers;
		}

		private string GetStringValue(XmlNode _node, string _attribute, bool required)
		{
			XmlNode a = _node.Attributes.RemoveNamedItem (_attribute);
			if (a == null) {
				if (required)
					throw new ConfigurationException ("Attribute required: " + _attribute);
				else
					return null;
			}
			return a.Value;		
		}
	}
}

--------------010606040901040709090906--