// 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.Properties; using Ringtoets.Common.Data.Calculation; using Ringtoets.Common.Data.DikeProfiles; using Ringtoets.Common.Data.Probabilistics; 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 CalculationConfigurationWriter where T : class, ICalculation { /// /// Writes a calculation configuration to an XML file. /// /// The calculation configuration to write. /// The path to the target XML file. /// Thrown when any parameter is null. /// Thrown when unable to write to . /// The itself will not be part of the written XML, only its children. public void Write(IEnumerable configuration, string filePath) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if (filePath == null) { throw new ArgumentNullException(nameof(filePath)); } 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 the in XML format to file. /// /// The dictionary of distributions, keyed on name, to write. /// The writer to use for writing. /// Thrown when is null. /// Thrown when the is closed. protected static void WriteDistributions(IDictionary distributions, XmlWriter writer) { if (distributions == null) { throw new ArgumentNullException(nameof(distributions)); } if (distributions.Count > 0) { writer.WriteStartElement(ConfigurationSchemaIdentifiers.StochastsElement); foreach (KeyValuePair keyValuePair in distributions) { WriteDistribution(keyValuePair.Value, keyValuePair.Key, writer); } writer.WriteEndElement(); } } /// /// Writes the properties of the in XML format to file. /// /// The break water to write. /// The writer to use for writing. /// Thrown when the is closed. protected static void WriteBreakWaterProperties(BreakWater breakWater, XmlWriter writer) { if (breakWater != null) { writer.WriteElementString( ConfigurationSchemaIdentifiers.BreakWaterType, BreakWaterTypeAsXmlString(breakWater.Type)); writer.WriteElementString( ConfigurationSchemaIdentifiers.BreakWaterHeight, XmlConvert.ToString(breakWater.Height)); } } /// /// 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 (ICalculationBase child in configuration) { var innerGroup = child as CalculationGroup; 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 static string BreakWaterTypeAsXmlString(BreakWaterType type) { return new BreakWaterTypeConverter().ConvertToInvariantString(type); } private static void WriteDistribution(IDistribution distribution, string elementName, XmlWriter writer) { writer.WriteStartElement(ConfigurationSchemaIdentifiers.StochastElement); writer.WriteAttributeString(ConfigurationSchemaIdentifiers.NameAttribute, elementName); writer.WriteElementString(ConfigurationSchemaIdentifiers.MeanElement, XmlConvert.ToString(distribution.Mean)); writer.WriteElementString(ConfigurationSchemaIdentifiers.StandardDeviationElement, XmlConvert.ToString(distribution.StandardDeviation)); writer.WriteEndElement(); } private void WriteCalculationGroup(CalculationGroup calculationGroup, XmlWriter writer) { writer.WriteStartElement(ConfigurationSchemaIdentifiers.FolderElement); writer.WriteAttributeString(ConfigurationSchemaIdentifiers.NameAttribute, calculationGroup.Name); WriteConfiguration(calculationGroup.Children, writer); writer.WriteEndElement(); } } }