// Copyright (C) Stichting Deltares 2016. All rights reserved. // // This file is part of Ringtoets. // // Ringtoets is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.Collections.Generic; using System.Xml; using Core.Common.IO.Exceptions; using Core.Common.Utils; using Core.Common.Utils.Properties; using Ringtoets.Common.IO.Configurations; using Ringtoets.Common.IO.Schema; namespace Ringtoets.Common.IO.Writers { /// /// Base implementation of writing calculation configurations to XML. /// /// The type of calculations which are written to file. public abstract class SchemaCalculationConfigurationWriter where T : class, IConfigurationItem { private readonly string filePath; /// /// Creates a new instance of . /// /// The path of the file to write to. /// Thrown when is invalid. /// A valid path: /// /// is not empty or null, /// does not consist out of only whitespace characters, /// does not contain an invalid character, /// does not end with a directory or path separator (empty file name). /// protected SchemaCalculationConfigurationWriter(string filePath) { this.filePath = filePath; IOUtils.ValidateFilePath(filePath); } /// /// Writes a calculation configuration to an XML file. /// /// The calculation configuration to write. /// Thrown when any parameter is null. /// Thrown when unable to write the file to the provided file path. public void Write(IEnumerable configuration) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } try { var settings = new XmlWriterSettings { Indent = true }; using (XmlWriter writer = XmlWriter.Create(filePath, settings)) { writer.WriteStartDocument(); writer.WriteStartElement(ConfigurationSchemaIdentifiers.ConfigurationElement); WriteConfiguration(configuration, writer); writer.WriteEndElement(); writer.WriteEndDocument(); } } catch (SystemException e) { throw new CriticalFileWriteException(string.Format(Resources.Error_General_output_error_0, filePath), e); } } /// /// Writes a single in XML format to file. /// /// The calculation to write. /// The writer to use for writing. /// Thrown when the is closed. protected abstract void WriteCalculation(T calculation, XmlWriter writer); /// /// Writes a distribution configuration when it has a value. /// /// The writer to use for writing. /// The name of the distribution. /// The configuration for the distribution that can be null. protected static void WriteDistributionWhenAvailable(XmlWriter writer, string distributionName, MeanVariationCoefficientStochastConfiguration configuration) { if (configuration != null) { writer.WriteDistribution(distributionName, configuration); } } /// /// Writes a distribution configuration when it has a value. /// /// The writer to use for writing. /// The name of the distribution. /// The configuration for the distribution that can be null. protected static void WriteDistributionWhenAvailable(XmlWriter writer, string distributionName, MeanStandardDeviationStochastConfiguration configuration) { if (configuration != null) { writer.WriteDistribution(distributionName, configuration); } } /// /// Writes an element with some content when the content has a value. /// /// The writer to use for writing. /// The name of the element. /// The content of the element that can be null. protected static void WriteElementWhenContentAvailable(XmlWriter writer, string elementName, string elementContent) { if (elementContent != null) { writer.WriteElementString( elementName, elementContent); } } /// /// Writes an element with some content when the content has a value. /// /// The writer to use for writing. /// The name of the element. /// The content of the element that can be null. protected static void WriteElementWhenContentAvailable(XmlWriter writer, string elementName, double? elementContent) { if (elementContent.HasValue) { writer.WriteElementString( elementName, XmlConvert.ToString(elementContent.Value)); } } /// /// Writes a wave reduction configuration when it has a value. /// /// The writer to use for writing. /// The configuration for the wave reduction that can be null. protected static void WriteWaveReductionWhenAvailable(XmlWriter writer, WaveReductionConfiguration configuration) { if (configuration != null) { writer.WriteWaveReduction(configuration); } } /// /// Writes the in XML format to file. /// /// The calculation group(s) and/or calculation(s) to write. /// The writer to use for writing. /// Thrown when /// contains a value that is neither nor . private void WriteConfiguration(IEnumerable configuration, XmlWriter writer) { foreach (IConfigurationItem child in configuration) { var innerGroup = child as CalculationGroupConfiguration; if (innerGroup != null) { WriteCalculationGroup(innerGroup, writer); } var calculation = child as T; if (calculation != null) { WriteCalculation(calculation, writer); } if (innerGroup == null && calculation == null) { throw new ArgumentException($"Cannot write calculation of type '{child.GetType()}' using this writer."); } } } private void WriteCalculationGroup(CalculationGroupConfiguration group, XmlWriter writer) { writer.WriteStartElement(ConfigurationSchemaIdentifiers.FolderElement); writer.WriteAttributeString(ConfigurationSchemaIdentifiers.NameAttribute, group.Name); WriteConfiguration(group.Items, writer); writer.WriteEndElement(); } } }