[Mono-devel-list] Bug + fix for Mono.Data.ProviderSectionHandler

Charles-Louis charlouis.mono at wanadoo.be
Fri Apr 25 08:02:15 EDT 2003


Hi,

I've discovered a bug in the ProviderSectionHandler when using MS.NET
FrameWork 1.1.

In the method Create, it was never tested wether the object found in
section.ChildNodes was a WhiteSpace or an Element, 
so I ran into some nasty exceptions...

	public virtual object Create(object parent,object configContext,
XmlNode section)
		{
			ProviderCollection providers=new ProviderCollection();
			foreach (XmlElement ProviderNode in section.ChildNodes)
			{
				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;
		}

So, I've made the following fix, and it works (changes are in red)

	public virtual object Create(object parent,object configContext,
XmlNode section)
		{
			ProviderCollection providers=new ProviderCollection();
			foreach (XmlElement ProviderNode in section.ChildNodes)
			{
        //Testing if it's an element and not a WhiteSpace
        if (ProviderNode.NodeType == XmlNodeType.Element)
        {
    				    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;
		}

I have attached the modified file.

-- 
Charles-Louis <charlouis.mono at wanadoo.be>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20030425/559469b6/attachment.html 
-------------- next part --------------
//
// Mono.Data.ProviderSectionHandler
//
// Authors:
//   Brian Ritchie (brianlritchie at hotmail.com) 
//  
//
// Copyright (C) Brian Ritchie, 2002
// 
//
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();
      foreach (XmlNode ProviderNode in section.ChildNodes)
			{
        if(ProviderNode.NodeType == XmlNodeType.Element)
        {
          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;		
		}
	}
}


More information about the Mono-devel-list mailing list