// 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.Xml; using Core.Common.IO.Exceptions; using Ringtoets.Common.Data.Calculation; using Ringtoets.Common.Data.Probabilistics; using Ringtoets.Common.IO.Schema; using Ringtoets.Piping.Data; using Ringtoets.Piping.IO.Schema; using CoreCommonUtilsResources = Core.Common.Utils.Properties.Resources; namespace Ringtoets.Piping.IO.Exporters { /// /// Writer for writing a piping configuration to XML. /// internal static class PipingConfigurationWriter { /// /// Writes a piping configuration to an XML file. /// /// The root calculation group containing the piping 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. internal static void Write(CalculationGroup rootCalculationGroup, string filePath) { if (rootCalculationGroup == null) { throw new ArgumentNullException(nameof(rootCalculationGroup)); } 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(rootCalculationGroup, writer); writer.WriteEndElement(); writer.WriteEndDocument(); } } catch (SystemException e) { throw new CriticalFileWriteException(string.Format(CoreCommonUtilsResources.Error_General_output_error_0, filePath), e); } } private static void WriteConfiguration(CalculationGroup calculationGroup, XmlWriter writer) { foreach (ICalculationBase child in calculationGroup.Children) { var innerGroup = child as CalculationGroup; if (innerGroup != null) { WriteCalculationGroup(innerGroup, writer); } var calculation = child as PipingCalculation; if (calculation != null) { WriteCalculation(calculation, writer); } } } private static void WriteCalculationGroup(CalculationGroup calculationGroup, XmlWriter writer) { writer.WriteStartElement(ConfigurationSchemaIdentifiers.FolderElement); writer.WriteAttributeString(ConfigurationSchemaIdentifiers.NameAttribute, calculationGroup.Name); WriteConfiguration(calculationGroup, writer); writer.WriteEndElement(); } private static void WriteCalculation(PipingCalculation calculation, XmlWriter writer) { writer.WriteStartElement(ConfigurationSchemaIdentifiers.CalculationElement); writer.WriteAttributeString(ConfigurationSchemaIdentifiers.NameAttribute, calculation.Name); PipingInput calculationInputParameters = calculation.InputParameters; if (calculationInputParameters.UseAssessmentLevelManualInput) { writer.WriteElementString(PipingConfigurationSchemaIdentifiers.AssessmentLevelElement, XmlConvert.ToString(calculationInputParameters.AssessmentLevel)); } else if (calculationInputParameters.HydraulicBoundaryLocation != null) { writer.WriteElementString(ConfigurationSchemaIdentifiers.HydraulicBoundaryLocationElement, calculationInputParameters.HydraulicBoundaryLocation.Name); } if (calculationInputParameters.SurfaceLine != null) { writer.WriteElementString(PipingConfigurationSchemaIdentifiers.SurfaceLineElement, calculationInputParameters.SurfaceLine.Name); writer.WriteElementString(PipingConfigurationSchemaIdentifiers.EntryPointLElement, XmlConvert.ToString(calculationInputParameters.EntryPointL)); writer.WriteElementString(PipingConfigurationSchemaIdentifiers.ExitPointLElement, XmlConvert.ToString(calculationInputParameters.ExitPointL)); } if (calculationInputParameters.StochasticSoilModel != null) { writer.WriteElementString(PipingConfigurationSchemaIdentifiers.StochasticSoilModelElement, calculationInputParameters.StochasticSoilModel.Name); if (calculationInputParameters.StochasticSoilProfile?.SoilProfile != null) { writer.WriteElementString(PipingConfigurationSchemaIdentifiers.StochasticSoilProfileElement, calculationInputParameters.StochasticSoilProfile.SoilProfile.Name); } } WriteDistributions(calculationInputParameters, writer); writer.WriteEndElement(); } private static void WriteDistributions(PipingInput calculationInputParameters, XmlWriter writer) { writer.WriteStartElement(ConfigurationSchemaIdentifiers.StochastsElement); WriteDistribution(calculationInputParameters.PhreaticLevelExit, PipingConfigurationSchemaIdentifiers.PhreaticLevelExitStochastName, writer); WriteDistribution(calculationInputParameters.DampingFactorExit, PipingConfigurationSchemaIdentifiers.DampingFactorExitStochastName, writer); writer.WriteEndElement(); } 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(); } } }