nicko 2005/08/23 04:33:32
Modified: src/Repository/Hierarchy XmlHierarchyConfigurator.cs
Log:
Update for LOG4NET-43. Updated error message to be more explicit on incorrect
type. Restricted empty string fix only to empty element nodes.
Revision Changes Path
1.19 +51 -10
logging-log4net/src/Repository/Hierarchy/XmlHierarchyConfigurator.cs
Index: XmlHierarchyConfigurator.cs
===================================================================
RCS file:
/home/cvs/logging-log4net/src/Repository/Hierarchy/XmlHierarchyConfigurator.cs,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- XmlHierarchyConfigurator.cs 22 Aug 2005 16:31:15 -0000 1.18
+++ XmlHierarchyConfigurator.cs 23 Aug 2005 11:33:32 -0000 1.19
@@ -725,20 +725,26 @@
}
else
{
- // No value specified
- Type defaultObjectType = null;
- if (propertyType.IsClass &&
!propertyType.IsAbstract)
- {
- defaultObjectType =
propertyType;
- }
+ object createdObject = null;
- object createdObject;
- if
(propertyType==System.Type.GetType("System.String"))
+ if (propertyType == typeof(string) &&
!HasAttributesOrElements(element))
{
- createdObject="";
+ // If the property is a string
and the element is empty (no attributes
+ // or child elements) then we
special case the object value to an empty string.
+ // This is necessary because
while the String is a class it does not have
+ // a default constructor that
creates an empty string, which is the behavior
+ // we are trying to simulate
and would be expected from CreateObjectFromXml
+ createdObject = "";
}
else
{
+ // No value specified
+ Type defaultObjectType = null;
+ if
(IsTypeConstructible(propertyType))
+ {
+ defaultObjectType =
propertyType;
+ }
+
createdObject =
CreateObjectFromXml(element, defaultObjectType, propertyType);
}
@@ -784,6 +790,41 @@
}
/// <summary>
+ /// Test if an element has no attributes or child elements
+ /// </summary>
+ /// <param name="element">the element to inspect</param>
+ /// <returns><c>true</c> if the element has any attributes or
child elements, <c>false</c> otherwise</returns>
+ private bool HasAttributesOrElements(XmlElement element)
+ {
+ foreach(XmlNode node in element.ChildNodes)
+ {
+ if (node.NodeType == XmlNodeType.Attribute ||
node.NodeType == XmlNodeType.Element)
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /// <summary>
+ /// Test if a <see cref="Type"/> is constructible with
<c>Activator.CreateInstance</c>.
+ /// </summary>
+ /// <param name="type">the type to inspect</param>
+ /// <returns><c>true</c> if the type is creatable using a
default constructor, <c>false</c> otherwise</returns>
+ private static bool IsTypeConstructible(Type type)
+ {
+ if (type.IsClass && !type.IsAbstract)
+ {
+ ConstructorInfo defaultConstructor =
type.GetConstructor(new Type[0]);
+ if (defaultConstructor != null &&
!defaultConstructor.IsAbstract && !defaultConstructor.IsPrivate)
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /// <summary>
/// Look for a method on the <paramref name="targetType"/> that
matches the <paramref name="name"/> supplied
/// </summary>
/// <param name="targetType">the type that has the
method</param>
@@ -882,7 +923,7 @@
{
if (defaultTargetType == null)
{
- LogLog.Error("XmlHierarchyConfigurator:
Object type not specified. Cannot create object.");
+ LogLog.Error("XmlHierarchyConfigurator:
Object type not specified. Cannot create object of type
["+typeConstraint.FullName+"]. Missing Value or Type.");
return null;
}
else