additional SPRNET-912 fix

This commit is contained in:
eeichinger
2008-10-29 14:29:11 +00:00
parent 26be3d4ef0
commit 62a976ffaa
5 changed files with 51 additions and 155 deletions

View File

@@ -235,9 +235,15 @@ namespace Spring.Objects.Factory.Xml
}
catch (XmlException ex)
{
throw new XmlObjectDefinitionStoreException(resource.Description,
throw new ObjectDefinitionStoreException(resource.Description,
"Line " + ex.LineNumber + " in XML document from " +
resource + " is invalid. " + ex.Message, ex);
resource + " is not well formed. " + ex.Message, ex);
}
catch (XmlSchemaException ex)
{
throw new ObjectDefinitionStoreException(resource.Description,
"Line " + ex.LineNumber + " in XML document from " +
resource + " violates the schema. " + ex.Message, ex);
}
catch(ObjectDefinitionStoreException)
{
@@ -258,16 +264,15 @@ namespace Spring.Objects.Factory.Xml
{
if (args.Severity == XmlSeverityType.Error)
{
#if !NET_1_0
XmlSchemaException ex = args.Exception;
#if !NET_2_0
// ignore validation errors for well-known 'xml' namespace. This seems to be a bug in net 1.0 + 1.1
if (args.Exception.Message.IndexOf("http://www.w3.org/XML/1998/namespace:") > -1)
if (ex.Message.IndexOf("http://www.w3.org/XML/1998/namespace:") > -1)
{
return;
}
throw new XmlException(args.Message, args.Exception, args.Exception.LineNumber, args.Exception.LinePosition);
#else
throw new XmlException(args.Message, args.Exception);
#endif
throw ex;
}
else
{

View File

@@ -1,121 +0,0 @@
using System;
using System.Runtime.Serialization;
using System.Xml;
namespace Spring.Objects.Factory.Xml
{
/// <summary>
/// XML-specific ObjectDefinitionStoreException subclass that wraps a XmlException, which
/// contains information about the error location.
/// </summary>
[Serializable]
public class XmlObjectDefinitionStoreException : ObjectDefinitionStoreException
{
#region Required for Standards Compliance
/// <summary>
/// Initializes a new instance of the <see cref="XmlObjectDefinitionStoreException"/> class.
/// </summary>
public XmlObjectDefinitionStoreException()
{
}
/// <summary>
/// Creates a new instance of the XmlObjectDefinitionStoreException class.
/// </summary>
/// <param name="message">
/// A message about the exception.
/// </param>
public XmlObjectDefinitionStoreException(string message)
: base(message)
{
}
/// <summary>
/// Creates a new instance of the XmlObjectDefinitionStoreException class.
/// </summary>
/// <param name="message">
/// A message about the exception.
/// </param>
/// <param name="rootCause">
/// The root exception that is being wrapped.
/// </param>
public XmlObjectDefinitionStoreException(string message, Exception rootCause)
: base(message, rootCause)
{
}
/// <summary>
/// Creates a new instance of the XmlObjectDefinitionStoreException class.
/// </summary>
/// <param name="info">
/// The <see cref="System.Runtime.Serialization.SerializationInfo"/>
/// that holds the serialized object data about the exception being thrown.
/// </param>
/// <param name="context">
/// The <see cref="System.Runtime.Serialization.StreamingContext"/>
/// that contains contextual information about the source or destination.
/// </param>
protected XmlObjectDefinitionStoreException(
SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
#endregion
/// <summary>
/// Initializes a new instance of the <see cref="XmlObjectDefinitionStoreException"/> class.
/// </summary>
/// <param name="resourceDescription">The description of the resource that the object definition came from</param>
/// <param name="msg">The detail message (used as exception message as-is).</param>
/// <param name="cause">The XmlException root cause.</param>
public XmlObjectDefinitionStoreException(string resourceDescription, string msg, XmlException cause)
: base(resourceDescription, msg, cause)
{
}
/// <summary>
/// Gets the line number in the XML resource that failed.
/// </summary>
/// <value>The line number if available (in case of a XmlException); -1 else.</value>
public int LineNumber
{
get
{
XmlException cause = InnerException as XmlException;
if (cause != null)
{
return (cause.LineNumber);
}
else
{
return -1;
}
}
}
/// <summary>
/// Gets the line position in the XML resource that failed.
/// </summary>
/// <value>The line position if available (in case of a XmlException); -1 else.</value>
public int LinePosition
{
get
{
XmlException cause = InnerException as XmlException;
if (cause != null)
{
return (cause.LinePosition);
}
else
{
return -1;
}
}
}
}
}

View File

@@ -2068,11 +2068,6 @@
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "Objects\Factory\Xml\XmlObjectDefinitionStoreException.cs"
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "Objects\Factory\Xml\XmlObjectFactory.cs"
SubType = "Code"

View File

@@ -122,17 +122,45 @@ namespace Spring.Objects.Factory.Xml
}
[Test]
[ExpectedException(typeof(XmlObjectDefinitionStoreException))]
public void ThrowsObjectDefinitionStoreExceptionOnValidationError()
{
DefaultListableObjectFactory of = new DefaultListableObjectFactory();
XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(of);
reader.LoadObjectDefinitions(new StringResource(
@"<?xml version='1.0' encoding='UTF-8' ?>
<objects xmlns='http://www.springframework.net'>
<INVALIDELEMENT id='test2' type='Spring.Objects.TestObject, Spring.Core.Tests' />
</objects>
"));
try
{
DefaultListableObjectFactory of = new DefaultListableObjectFactory();
XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(of);
reader.LoadObjectDefinitions(new StringResource(
@"<?xml version='1.0' encoding='UTF-8' ?>
<objects xmlns='http://www.springframework.net'>
<INVALIDELEMENT id='test2' type='Spring.Objects.TestObject, Spring.Core.Tests' />
</objects>
"));
Assert.Fail();
}
catch(ObjectDefinitionStoreException ex)
{
Assert.IsTrue( ex.Message.IndexOf("Line 3 in XML document from violates the schema.") > -1);
}
}
[Test]
public void ThrowsObjectDefinitionStoreExceptionOnInvalidXml()
{
try
{
DefaultListableObjectFactory of = new DefaultListableObjectFactory();
XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(of);
reader.LoadObjectDefinitions(new StringResource(
@"<?xml version='1.0' encoding='UTF-8' ?>
<objects xmlns='http://www.springframework.net'>
<object id='test2' type='Spring.Objects.TestObject, Spring.Core.Tests'>
</objects>
"));
Assert.Fail();
}
catch(ObjectDefinitionStoreException ex)
{
Assert.IsTrue( ex.Message.IndexOf("Line 4 in XML document from is not well formed.") > -1);
}
}
#region ThrowsObjectDefinitionStoreExceptionOnErrorDuringObjectDefinitionRegistration Helper

View File

@@ -993,15 +993,10 @@ namespace Spring.Objects.Factory.Xml
reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("invalid.xml", GetType()));
Assert.Fail("Should have thrown XmlObjectDefinitionStoreException");
}
#if !NET_1_0
catch (XmlObjectDefinitionStoreException e)
catch (ObjectDefinitionStoreException e)
{
Assert.AreEqual(0, e.Message.IndexOf("Line 21 in XML document"));
}
#else
catch (XmlObjectDefinitionStoreException)
{}
#endif
}
[Test]
@@ -1014,16 +1009,10 @@ namespace Spring.Objects.Factory.Xml
reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("invalid.xml", GetType()));
Assert.Fail("Should have thrown XmlObjectDefinitionStoreException");
}
#if !NET_1_0
catch (XmlObjectDefinitionStoreException e)
catch (ObjectDefinitionStoreException e)
{
Assert.AreEqual(0, e.Message.IndexOf("Line 21 in XML document"));
}
#else
catch (XmlObjectDefinitionStoreException)
{}
#endif
}
[Test]