Index: Riskeer/Common/src/Riskeer.Common.IO/Configurations/Import/CalculationConfigurationMigrator.cs =================================================================== diff -u -rced4d3286ef6a071e8b29ffc3998ca319d4a2120 -r943997f6ebeee5d581f7f1f828a236568f7323f2 --- Riskeer/Common/src/Riskeer.Common.IO/Configurations/Import/CalculationConfigurationMigrator.cs (.../CalculationConfigurationMigrator.cs) (revision ced4d3286ef6a071e8b29ffc3998ca319d4a2120) +++ Riskeer/Common/src/Riskeer.Common.IO/Configurations/Import/CalculationConfigurationMigrator.cs (.../CalculationConfigurationMigrator.cs) (revision 943997f6ebeee5d581f7f1f828a236568f7323f2) @@ -64,13 +64,9 @@ using (var writer = XmlWriter.Create(stringBuilder)) { transformer.Transform(xmlDocument.CreateReader(ReaderOptions.None), writer); - writer.Close(); - writer.Flush(); } } - catch (Exception e) when (e is InvalidOperationException - || e is XsltException - || e is IOException) + catch (Exception e) { throw new CalculationConfigurationMigrationException(e.Message, e); } Index: Riskeer/Common/src/Riskeer.Common.IO/Configurations/Import/CalculationConfigurationReader.cs =================================================================== diff -u -r20b6dad5d55edd4654a0554d9f15bcc634098520 -r943997f6ebeee5d581f7f1f828a236568f7323f2 --- Riskeer/Common/src/Riskeer.Common.IO/Configurations/Import/CalculationConfigurationReader.cs (.../CalculationConfigurationReader.cs) (revision 20b6dad5d55edd4654a0554d9f15bcc634098520) +++ Riskeer/Common/src/Riskeer.Common.IO/Configurations/Import/CalculationConfigurationReader.cs (.../CalculationConfigurationReader.cs) (revision 943997f6ebeee5d581f7f1f828a236568f7323f2) @@ -43,6 +43,7 @@ where TReadCalculation : IConfigurationItem { private const string defaultSchemaName = "ConfiguratieSchema.xsd"; + private readonly string xmlFilePath; private XDocument xmlDocument; @@ -75,22 +76,19 @@ IOUtils.ValidateFilePath(xmlFilePath); - ValidateFileExists(xmlFilePath); + this.xmlFilePath = xmlFilePath; - xmlDocument = LoadDocument(xmlFilePath); + ValidateFileExists(); - CalculationConfigurationSchemaDefinition schemaDefinition = GetSchemaDefinition(schemaDefinitions, xmlFilePath); + xmlDocument = LoadDocument(); - ValidateToSchema(xmlDocument, xmlFilePath, schemaDefinition.MainSchemaDefinition, schemaDefinition.NestedSchemaDefinitions); + CalculationConfigurationSchemaDefinition schemaDefinition = GetSchemaDefinition(schemaDefinitions); - ValidateNotEmpty(xmlDocument, xmlFilePath); + ValidateToSchema(schemaDefinition.MainSchemaDefinition, schemaDefinition.NestedSchemaDefinitions); - int index = Array.IndexOf(schemaDefinitions, schemaDefinition); + ValidateNotEmpty(); - for (int i = index + 1; i < schemaDefinitions.Length; i++) - { - MigrateToNewSchema(schemaDefinitions[i].MigrationScript, xmlFilePath); - } + MigrateWhenNeeded(schemaDefinitions, schemaDefinition); } /// @@ -113,11 +111,10 @@ /// Gets the correct schema definition depending on the version. /// /// All the schema definitions. - /// The file path to the XML file. /// The schema definition that belongs to the XML file. /// Thrown when the version /// from the XML file is not supported. - private CalculationConfigurationSchemaDefinition GetSchemaDefinition(IEnumerable schemaDefinitions, string xmlFilePath) + private CalculationConfigurationSchemaDefinition GetSchemaDefinition(IEnumerable schemaDefinitions) { int versionNumber; @@ -149,12 +146,30 @@ } /// + /// Migrates the XML document to newer versions when needed/ + /// + /// All the schema definitions. + /// The schema definition for the version + /// of the XML document. + /// Thrown when something goes wrong + /// while migrating. + private void MigrateWhenNeeded(CalculationConfigurationSchemaDefinition[] schemaDefinitions, CalculationConfigurationSchemaDefinition schemaDefinition) + { + int index = Array.IndexOf(schemaDefinitions, schemaDefinition); + + for (int i = index + 1; i < schemaDefinitions.Length; i++) + { + MigrateToNewSchema(schemaDefinitions[i].MigrationScript); + } + } + + /// /// Migrates the with the given . /// /// The script to perform the migration with. /// Thrown when something goes wrong /// while migrating. - private void MigrateToNewSchema(string migrationScript, string xmlFilePath) + private void MigrateToNewSchema(string migrationScript) { try { @@ -170,11 +185,10 @@ } /// - /// Validates whether a file exists at the provided . + /// Validates whether a file exists at the . /// - /// The file path to validate. /// Thrown when no existing file is found. - private static void ValidateFileExists(string xmlFilePath) + private void ValidateFileExists() { if (!File.Exists(xmlFilePath)) { @@ -186,11 +200,11 @@ } /// - /// Loads an XML document from the provided . + /// Loads an XML document from the . /// - /// The file path to load the XML document from. - /// Thrown when the XML document cannot be loaded. - private static XDocument LoadDocument(string xmlFilePath) + /// Thrown when + /// the XML document cannot be loaded. + private XDocument LoadDocument() { try { @@ -214,19 +228,16 @@ } /// - /// Validates the provided XML document based on the provided schema definitions. + /// Validates the XML document based on the provided schema definitions. /// - /// The XML document to validate. - /// The file path the XML document is loaded from. /// A string representing the main schema definition. /// A containing /// zero to more nested schema definitions - /// Thrown when the provided XML document does not match + /// Thrown when the XML document does not match /// the provided schema definitions. /// Thrown when does not /// reference the default schema definition ConfiguratieSchema.xsd. - private static void ValidateToSchema(XDocument document, string xmlFilePath, string mainSchemaDefinition, - IDictionary nestedSchemaDefinitions) + private void ValidateToSchema(string mainSchemaDefinition, IDictionary nestedSchemaDefinitions) { if (!mainSchemaDefinition.Contains(defaultSchemaName)) { @@ -240,7 +251,7 @@ try { - combinedXmlSchemaDefinition.Validate(document); + combinedXmlSchemaDefinition.Validate(xmlDocument); } catch (XmlSchemaValidationException exception) { @@ -254,16 +265,15 @@ } /// - /// Validates whether the provided XML document is not empty. + /// Validates whether the XML document is not empty. /// - /// The XML document to validate. - /// The file path the XML document is loaded from. - /// Thrown when the provided XML document does not contain configuration items. - private static void ValidateNotEmpty(XDocument document, string xmlFilePath) + /// Thrown when the XML document + /// does not contain configuration items. + private void ValidateNotEmpty() { - if (!document.Descendants() - .Any(d => d.Name == ConfigurationSchemaIdentifiers.CalculationElement - || d.Name == ConfigurationSchemaIdentifiers.FolderElement)) + if (!xmlDocument.Descendants() + .Any(d => d.Name == ConfigurationSchemaIdentifiers.CalculationElement + || d.Name == ConfigurationSchemaIdentifiers.FolderElement)) { string message = new FileReaderErrorMessageBuilder(xmlFilePath) .Build(Resources.CalculationConfigurationReader_No_configuration_items_found);