Index: Ringtoets/Common/src/Ringtoets.Common.IO/Readers/ConfigurationReader.cs
===================================================================
diff -u -re1714dc7e1c63c28c493be6dcb06b38bf1e1cb0e -rb36040fa7764b4eeb2017c931f7741cfda7be389
--- Ringtoets/Common/src/Ringtoets.Common.IO/Readers/ConfigurationReader.cs (.../ConfigurationReader.cs) (revision e1714dc7e1c63c28c493be6dcb06b38bf1e1cb0e)
+++ Ringtoets/Common/src/Ringtoets.Common.IO/Readers/ConfigurationReader.cs (.../ConfigurationReader.cs) (revision b36040fa7764b4eeb2017c931f7741cfda7be389)
@@ -23,9 +23,11 @@
using System.IO;
using System.Xml;
using System.Xml.Linq;
+using System.Xml.Schema;
using Core.Common.IO.Exceptions;
using Core.Common.Utils;
using Core.Common.Utils.Builders;
+using Ringtoets.Common.IO.Properties;
using CoreCommonUtilsResources = Core.Common.Utils.Properties.Resources;
namespace Ringtoets.Common.IO.Readers
@@ -42,6 +44,7 @@
/// Creates a new instance of .
///
/// The file path to the XML file.
+ /// A file reference towards an XML Schema Definition (XSD).
/// Thrown when is invalid.
/// Thrown when:
///
@@ -50,13 +53,15 @@
/// - points to a file that does not pass the schema validation.
///
///
- protected ConfigurationReader(string xmlFilePath)
+ protected ConfigurationReader(string xmlFilePath, string schemaResXFileRef)
{
IOUtils.ValidateFilePath(xmlFilePath);
ValidateFileExists(xmlFilePath);
xmlDocument = LoadDocument(xmlFilePath);
+
+ ValidateToSchema(xmlDocument, schemaResXFileRef, xmlFilePath);
}
///
@@ -97,5 +102,32 @@
throw new CriticalFileReadException(message, exception);
}
}
+
+ ///
+ /// Validates the provided XML document based on the provided XML Schema Definition (XSD).
+ ///
+ /// The XML document to validate.
+ /// A file reference towards the XML Schema Definition (XSD) to use for the validation.
+ /// The file path the XML document is loaded from.
+ /// Thrown when the provided XML document does not match the provided XML Schema Definition (XSD).
+ private static void ValidateToSchema(XDocument document, string schemaResXFileRef, string xmlFilePath)
+ {
+ var xmlSchemaSet = new XmlSchemaSet();
+ xmlSchemaSet.Add(XmlSchema.Read(new StringReader(schemaResXFileRef), null));
+
+ try
+ {
+ document.Validate(xmlSchemaSet, null);
+ }
+ catch (XmlSchemaValidationException exception)
+ {
+ string message = string.Format(Resources.ConfigurationReader_Configuration_contains_no_valid_xml_line_0_position_1_reason_2,
+ exception.LineNumber,
+ exception.LinePosition,
+ exception.Message);
+
+ throw new CriticalFileReadException(new FileReaderErrorMessageBuilder(xmlFilePath).Build(message), exception);
+ }
+ }
}
}
\ No newline at end of file