// 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 CoreCommonUtilsResources = Core.Common.Utils.Properties.Resources; namespace Ringtoets.Integration.Forms { /// /// Writer class for . /// public class WmtsConnectionInfoWriter { private readonly string filePath; /// /// Creates a new instance of . /// /// Path 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). /// public WmtsConnectionInfoWriter(string filePath) { IOUtils.ValidateFilePath(filePath); this.filePath = filePath; } /// /// Writes the to . /// /// The objects to write. /// Thrown when is null. /// Thrown when writing /// to failed. public void WriteWmtsConnectionInfo(IEnumerable wmtsConnectionInfos) { if (wmtsConnectionInfos == null) { throw new ArgumentNullException(nameof(wmtsConnectionInfos)); } try { WriteWmtsConnectionInfosToXml(wmtsConnectionInfos); } catch (Exception exception) { throw new CriticalFileWriteException(string.Format(CoreCommonUtilsResources.Error_General_output_error_0, filePath), exception); } } private void WriteWmtsConnectionInfosToXml(IEnumerable wmtsConnectionInfos) { using (XmlWriter writer = XmlWriter.Create(filePath)) { writer.WriteStartDocument(); writer.WriteStartElement(WmtsConnectionInfoXmlDefinitions.RootElement); foreach (WmtsConnectionInfo wmtsConnectionInfo in wmtsConnectionInfos) { WriteWmtsConnectionInfoToXml(writer, wmtsConnectionInfo); } writer.WriteEndElement(); writer.WriteEndDocument(); writer.Flush(); writer.Close(); } } private static void WriteWmtsConnectionInfoToXml(XmlWriter writer, WmtsConnectionInfo wmtsConnectionInfo) { writer.WriteStartElement(WmtsConnectionInfoXmlDefinitions.WmtsConnectionElement); writer.WriteElementString(WmtsConnectionInfoXmlDefinitions.WmtsConnectionNameElement, wmtsConnectionInfo.Name); writer.WriteElementString(WmtsConnectionInfoXmlDefinitions.WmtsConnectionUrlElement, wmtsConnectionInfo.Url); writer.WriteEndElement(); } } }