// Copyright (C) Stichting Deltares 2020. All rights reserved.
//
// This file is part of the Dam Engine.
//
// The Dam Engine is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero 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.IO;
using System.Reflection;
using System.Text;
using System.Xml.Serialization;
namespace Deltares.LayerOnSlopeTool.Io
{
/// Serialize and de-serialize form XML
/// DGSMStabDam.dll requires XML-element identifiers which are prefixed with "tns:" and "tnsb:".
/// These prefixes cannot be handled by the automatic XML serializer object generation with XSD.exe
/// So we apply pre- and post- processors to add and remove these prefixes.
/// Also the XML identification header has to be changed to accomodate this.
public class MStabDamXmlSerializer
{
private const string tnsPrefixSrc = "tnsPrefix";
private const string tnsPrefixDest = "tns:";
private const string tnsbPrefixSrc = "tnsbPrefix";
private const string tnsbPrefixDest = "tnsb:";
private const string idLineSrc =
@"";
private const string idLineDest =
@"";
///
/// Saves the tnsPrefixDamMStabDoc as XML.
///
/// The filename.
/// The tnsPrefixDamMStabDoc.
public static void SaveDamMStabDocAsXmlFile(string filename, tnsPrefixDamMStabDoc tnsPrefixDamMStabDoc)
{
string xmlString = SaveDamMStabDocAsXmlString(tnsPrefixDamMStabDoc);
File.WriteAllText(filename, xmlString, Encoding.ASCII);
}
///
/// Loads the tnsPrefixDamMStabDoc from XML.
///
/// The filename.
/// tnsPrefixDamMStabDoc object
public static tnsPrefixDamMStabDoc LoadDamMStabDocFromXmlFile(string filename)
{
string xmlString = File.ReadAllText(filename);
tnsPrefixDamMStabDoc tnsPrefixDamMStabDoc = LoadDamMStabDocFromXmlString(xmlString);
return tnsPrefixDamMStabDoc;
}
///
/// Saves the tnsPrefixDamMStabDoc as XML string.
///
/// The tnsPrefixDamMStabDoc.
/// xm string
public static string SaveDamMStabDocAsXmlString(tnsPrefixDamMStabDoc tnsPrefixDamMStabDoc)
{
tnsPrefixDamMStabDoc.tnsPrefixFileIdentification = CreateFileIdentification();
XmlSerializer xmlSerializer = new XmlSerializer(typeof(tnsPrefixDamMStabDoc));
TextWriter writer = new ExtentedStringWriter(new StringBuilder(), Encoding.UTF8);
xmlSerializer.Serialize(writer, tnsPrefixDamMStabDoc);
string xmlString = writer.ToString();
xmlString = PreProcessPrefixes(xmlString);
return xmlString;
}
private static string PreProcessPrefixes(string xmlString)
{
xmlString = xmlString.Replace(tnsbPrefixSrc, tnsbPrefixDest);
xmlString = xmlString.Replace(tnsPrefixSrc, tnsPrefixDest);
xmlString = xmlString.Replace(idLineSrc, idLineDest);
return xmlString;
}
///
/// Loads the tnsPrefixDamMStabDoc from XML string.
///
/// The XML string.
/// tnsPrefixDamMStabDoc object
public static tnsPrefixDamMStabDoc LoadDamMStabDocFromXmlString(string xmlString)
{
xmlString = PostProcessPrefixes(xmlString);
tnsPrefixDamMStabDoc tnsPrefixDamMStabDoc;
XmlSerializer xmlSerializer = new XmlSerializer(typeof(tnsPrefixDamMStabDoc));
StringReader reader = new StringReader(xmlString);
tnsPrefixDamMStabDoc = (tnsPrefixDamMStabDoc)xmlSerializer.Deserialize(reader);
return tnsPrefixDamMStabDoc;
}
private static string PostProcessPrefixes(string xmlString)
{
xmlString = xmlString.Replace(tnsbPrefixDest, tnsbPrefixSrc);
xmlString = xmlString.Replace(tnsPrefixDest, tnsPrefixSrc);
xmlString = xmlString.Replace(idLineDest, idLineSrc);
return xmlString;
}
private static tnsPrefixDamMStabDocTnsPrefixFileIdentification CreateFileIdentification()
{
tnsPrefixDamMStabDocTnsPrefixFileIdentification tnsPrefixDamMStabDocFileIdentification = new tnsPrefixDamMStabDocTnsPrefixFileIdentification();
var hostAssembly = Assembly.GetEntryAssembly();
if (hostAssembly == null)
{
hostAssembly = Assembly.GetExecutingAssembly();
}
tnsPrefixDamMStabDocFileIdentification.Application = hostAssembly.GetName().Name;
tnsPrefixDamMStabDocFileIdentification.Version = hostAssembly.GetName().Version.ToString();
tnsPrefixDamMStabDocFileIdentification.Created = DateTime.Now.ToString("yyyy-MM-dd") + "T"+ DateTime.Now.ToString("HH:MM:s") + "Z";
tnsPrefixDamMStabDocFileIdentification.Company = "Deltares";
return tnsPrefixDamMStabDocFileIdentification;
}
}
}