diff --git a/src/Spring/Spring.Core/Objects/Factory/Xml/XmlObjectDefinitionReader.cs b/src/Spring/Spring.Core/Objects/Factory/Xml/XmlObjectDefinitionReader.cs index f010e8e3..ee1d93d6 100644 --- a/src/Spring/Spring.Core/Objects/Factory/Xml/XmlObjectDefinitionReader.cs +++ b/src/Spring/Spring.Core/Objects/Factory/Xml/XmlObjectDefinitionReader.cs @@ -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 { diff --git a/src/Spring/Spring.Core/Objects/Factory/Xml/XmlObjectDefinitionStoreException.cs b/src/Spring/Spring.Core/Objects/Factory/Xml/XmlObjectDefinitionStoreException.cs deleted file mode 100644 index 0afbcf4e..00000000 --- a/src/Spring/Spring.Core/Objects/Factory/Xml/XmlObjectDefinitionStoreException.cs +++ /dev/null @@ -1,121 +0,0 @@ - - -using System; -using System.Runtime.Serialization; -using System.Xml; - -namespace Spring.Objects.Factory.Xml -{ - /// - /// XML-specific ObjectDefinitionStoreException subclass that wraps a XmlException, which - /// contains information about the error location. - /// - [Serializable] - public class XmlObjectDefinitionStoreException : ObjectDefinitionStoreException - { - - #region Required for Standards Compliance - /// - /// Initializes a new instance of the class. - /// - public XmlObjectDefinitionStoreException() - { - } - - /// - /// Creates a new instance of the XmlObjectDefinitionStoreException class. - /// - /// - /// A message about the exception. - /// - public XmlObjectDefinitionStoreException(string message) - : base(message) - { - } - - /// - /// Creates a new instance of the XmlObjectDefinitionStoreException class. - /// - /// - /// A message about the exception. - /// - /// - /// The root exception that is being wrapped. - /// - public XmlObjectDefinitionStoreException(string message, Exception rootCause) - : base(message, rootCause) - { - } - - /// - /// Creates a new instance of the XmlObjectDefinitionStoreException class. - /// - /// - /// The - /// that holds the serialized object data about the exception being thrown. - /// - /// - /// The - /// that contains contextual information about the source or destination. - /// - protected XmlObjectDefinitionStoreException( - SerializationInfo info, StreamingContext context) - : base(info, context) - { - } - - #endregion - - /// - /// Initializes a new instance of the class. - /// - /// The description of the resource that the object definition came from - /// The detail message (used as exception message as-is). - /// The XmlException root cause. - public XmlObjectDefinitionStoreException(string resourceDescription, string msg, XmlException cause) - : base(resourceDescription, msg, cause) - { - - } - - /// - /// Gets the line number in the XML resource that failed. - /// - /// The line number if available (in case of a XmlException); -1 else. - public int LineNumber - { - get - { - XmlException cause = InnerException as XmlException; - if (cause != null) - { - return (cause.LineNumber); - } - else - { - return -1; - } - } - } - - /// - /// Gets the line position in the XML resource that failed. - /// - /// The line position if available (in case of a XmlException); -1 else. - public int LinePosition - { - get - { - XmlException cause = InnerException as XmlException; - if (cause != null) - { - return (cause.LinePosition); - } - else - { - return -1; - } - } - } - } -} \ No newline at end of file diff --git a/src/Spring/Spring.Core/Spring.Core.2003.csproj b/src/Spring/Spring.Core/Spring.Core.2003.csproj index 900ad6ba..26036771 100644 --- a/src/Spring/Spring.Core/Spring.Core.2003.csproj +++ b/src/Spring/Spring.Core/Spring.Core.2003.csproj @@ -2068,11 +2068,6 @@ SubType = "Code" BuildAction = "Compile" /> - - - - -")); + try + { + DefaultListableObjectFactory of = new DefaultListableObjectFactory(); + XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(of); + reader.LoadObjectDefinitions(new StringResource( + @" + + + + ")); + 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( + @" + + + + ")); + Assert.Fail(); + } + catch(ObjectDefinitionStoreException ex) + { + Assert.IsTrue( ex.Message.IndexOf("Line 4 in XML document from is not well formed.") > -1); + } } #region ThrowsObjectDefinitionStoreExceptionOnErrorDuringObjectDefinitionRegistration Helper diff --git a/test/Spring/Spring.Core.Tests/Objects/Factory/Xml/XmlObjectFactoryTests.cs b/test/Spring/Spring.Core.Tests/Objects/Factory/Xml/XmlObjectFactoryTests.cs index 97216537..f67425b9 100644 --- a/test/Spring/Spring.Core.Tests/Objects/Factory/Xml/XmlObjectFactoryTests.cs +++ b/test/Spring/Spring.Core.Tests/Objects/Factory/Xml/XmlObjectFactoryTests.cs @@ -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]