Index: Ringtoets/Common/src/Ringtoets.Common.IO/Readers/ConfigurationReader.cs
===================================================================
diff -u -r3d1a79d25b4059b58271f549f62fc48ac1d1a3ec -re8d6d0512bc28b0af2bd1e8fae1f0bd9e976300c
--- Ringtoets/Common/src/Ringtoets.Common.IO/Readers/ConfigurationReader.cs (.../ConfigurationReader.cs) (revision 3d1a79d25b4059b58271f549f62fc48ac1d1a3ec)
+++ Ringtoets/Common/src/Ringtoets.Common.IO/Readers/ConfigurationReader.cs (.../ConfigurationReader.cs) (revision e8d6d0512bc28b0af2bd1e8fae1f0bd9e976300c)
@@ -23,8 +23,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
-using System.Net;
-using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
@@ -74,16 +72,11 @@
{
IOUtils.ValidateFilePath(xmlFilePath);
- if (string.IsNullOrWhiteSpace(mainSchemaDefinition))
- {
- throw new ArgumentException(nameof(mainSchemaDefinition));
- }
-
ValidateFileExists(xmlFilePath);
xmlDocument = LoadDocument(xmlFilePath);
- ValidateToSchema(xmlDocument, mainSchemaDefinition, xmlFilePath);
+ ValidateToSchema(xmlDocument, xmlFilePath, mainSchemaDefinition, nestedSchemaDefinitions);
ValidateNotEmpty(xmlDocument, xmlFilePath);
}
@@ -147,36 +140,24 @@
/// Validates the provided XML document based on the provided XML Schema Definition (XSD).
///
/// The XML document to validate.
- /// A string representing the XML Schema Definition (XSD) to use for the validation.
+ /// A string representing the main schema definition.
+ /// A containing
+ /// one or more nested schema definitions
/// 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 schemaString, string xmlFilePath)
+ private static void ValidateToSchema(XDocument document, string xmlFilePath, string mainSchemaDefinition, IDictionary nestedSchemaDefinitions)
{
- var xmlResourcesResolver = new XmlResourcesResolver();
- var xmlSchemaSet = new XmlSchemaSet
- {
- XmlResolver = xmlResourcesResolver
- };
+ var combinedXmlSchemaDefinition = new CombinedXmlSchemaDefinition(mainSchemaDefinition, nestedSchemaDefinitions
+ .Concat(new[]
+ {
+ new KeyValuePair("ConfiguratieSchema.xsd", Resources.ConfiguratieSchema)
+ })
+ .ToDictionary(kv => kv.Key, kv => kv.Value));
try
{
- xmlSchemaSet.Add(XmlSchema.Read(new StringReader(schemaString), null));
+ combinedXmlSchemaDefinition.Validate(document);
}
- catch (Exception exception) when (exception is XmlException
- || exception is XmlSchemaException)
- {
- throw new ArgumentException($"Invalid 'schemaString': {exception.Message}", exception);
- }
-
- if (!xmlResourcesResolver.BaseSchemeReferenced)
- {
- throw new ArgumentException("Invalid 'schemaString': the base XML Schema Definition file 'ConfiguratieSchema.xsd' is not referenced.");
- }
-
- 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,
@@ -228,34 +209,5 @@
return new ReadCalculationGroup(folderElement.Attribute(ConfigurationSchemaIdentifiers.NameAttribute)?.Value,
ParseElements(folderElement.Elements()));
}
-
- private class XmlResourcesResolver : XmlResolver
- {
- public override ICredentials Credentials
- {
- set
- {
- throw new NotImplementedException();
- }
- }
-
- public bool BaseSchemeReferenced { get; private set; }
-
- public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
- {
- switch (Path.GetFileName(absoluteUri.ToString()))
- {
- case "ConfiguratieSchema.xsd":
- BaseSchemeReferenced = true;
- return new MemoryStream(Encoding.UTF8.GetBytes(Resources.ConfiguratieSchema));
- case "StochastSchema.xsd":
- return new MemoryStream(Encoding.UTF8.GetBytes(Resources.StochastSchema));
- case "HrLocatieSchema.xsd":
- return new MemoryStream(Encoding.UTF8.GetBytes(Resources.HrLocatieSchema));
- }
-
- return null;
- }
- }
}
}
\ No newline at end of file