// 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 Lesser 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser 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.IO;
using System.Xml;
using Core.Common.IO.Exceptions;
using Core.Common.Utils;
using CoreCommonUtilsResources = Core.Common.Utils.Properties.Resources;
namespace Core.Components.Gis.IO.Writers
{
///
/// 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 (SystemException exception)
{
throw new CriticalFileWriteException(string.Format(CoreCommonUtilsResources.Error_General_output_error_0, filePath), exception);
}
}
private void WriteWmtsConnectionInfosToXml(IEnumerable wmtsConnectionInfos)
{
EnsureParentDirectoryExists();
var settings = new XmlWriterSettings
{
Indent = true
};
using (XmlWriter writer = XmlWriter.Create(filePath, settings))
{
writer.WriteStartDocument();
writer.WriteStartElement(WmtsConnectionInfoXmlDefinitions.RootElement);
foreach (WmtsConnectionInfo wmtsConnectionInfo in wmtsConnectionInfos)
{
WriteWmtsConnectionInfoToXml(writer, wmtsConnectionInfo);
}
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
}
}
///
/// Creates the parent folder of if it does not exist.
///
/// Thrown when the network name is not known.
/// Thrown when the caller does not have the
/// required permission.
/// Thrown when the specified path, file name, or
/// both exceed the system-defined maximum length.
/// Thrown when the specified path is invalid.
private void EnsureParentDirectoryExists()
{
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
}
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();
}
}
}