Skip to content

Commit

Permalink
Add management of a solution #582
Browse files Browse the repository at this point in the history
- working on: copy required functionality from ASMD
- UT 👍
  • Loading branch information
mpostol committed Apr 28, 2021
1 parent 72719f6 commit e71824a
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Common/Infrastructure/Serializers/IStylesheetNameProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//____________________________________________________________________________
//
// Copyright (C) 2021, Mariusz Postol LODZ POLAND.
//
// To be in touch join the community at GITTER: https://gitter.im/mpostol/TP
//____________________________________________________________________________

namespace UAOOI.Common.Infrastructure.Serializers
{
/// <summary>
/// Represents XML file style sheet name provider
/// </summary>
public interface IStylesheetNameProvider
{
/// <summary>
/// The style sheet name
/// </summary>
string StylesheetName { get; }
}
}
91 changes: 91 additions & 0 deletions Common/Infrastructure/Serializers/XmlFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//____________________________________________________________________________
//
// Copyright (C) 2021, Mariusz Postol LODZ POLAND.
//
// To be in touch join the community at GITTER: https://gitter.im/mpostol/TP
//____________________________________________________________________________

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace UAOOI.Common.Infrastructure.Serializers
{
/// <summary>
/// Provides static methods for serialization objects into XML documents and writing the XML document to a file.
/// </summary>
internal static class XmlFile
{

#region public
/// <summary>
/// Serializes the specified <paramref name="dataObject"/> and writes the XML document to a file.
/// </summary>
/// <typeparam name="type">The type of the root object to be serialized and saved in the file.</typeparam>
/// <param name="dataObject">The object containing working data to be serialized and saved in the file.</param>
/// <param name="path">A relative or absolute path for the file containing the serialized object.</param>
/// <param name="mode">Specifies how the operating system should open a file <see cref="FileMode"/>.</param>
/// <param name="stylesheetName">Name of the stylesheet document.</param>
/// <exception cref="System.ArgumentNullException">
/// path
/// or
/// dataObject
/// or
/// stylesheetName
/// </exception>
public static void WriteXmlFile<type>(type dataObject, string path, FileMode mode, string stylesheetName)
{
if (string.IsNullOrEmpty(path))
throw new ArgumentNullException(nameof(path));
if (dataObject == null)
throw new ArgumentNullException(nameof(dataObject));
XmlSerializer _xmlSerializer = new XmlSerializer(typeof(type));
XmlWriterSettings _setting = new XmlWriterSettings()
{
Indent = true,
IndentChars = " ",
NewLineChars = "\r\n"
};
using (FileStream _docStream = new FileStream(path, mode, FileAccess.Write))
{
XmlWriter _writer = XmlWriter.Create(_docStream, _setting);
if (!string.IsNullOrEmpty(stylesheetName))
_writer.WriteProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" " + string.Format("href=\"{0}\"", stylesheetName));
_xmlSerializer.Serialize(_writer, dataObject);
}
}
/// <summary>
/// Serializes the specified <paramref name="dataObject"/> and writes the XML document to a file.
/// </summary>
/// <typeparam name="type">The type of the object to be serialized and saved in the file.</typeparam>
/// <param name="dataObject">The object containing working data to be serialized and saved in the file.</param>
/// <param name="path">A relative or absolute path for the file containing the serialized object.</param>
/// <param name="mode">Specifies how the operating system should open a file.</param>
public static void WriteXmlFile<type>(type dataObject, string path, FileMode mode)
where type : IStylesheetNameProvider
{
XmlFile.WriteXmlFile<type>(dataObject, path, mode, dataObject.StylesheetName);
}
/// <summary>
/// Reads an XML document from the file <paramref name="path"/> and deserializes its content to returned object.
/// </summary>
/// <typeparam name="type">The type of the object to be deserialized and saved in the file.</typeparam>
/// <param name="path">A relative or absolute path for the file containing the serialized object.</param>
/// <returns>An object containing working data retrieved from an XML file.</returns>
/// <exception cref="System.ArgumentNullException">path</exception>
public static type ReadXmlFile<type>(string path)
{
if (string.IsNullOrEmpty(path))
throw new ArgumentNullException(nameof(path));
type _content = default(type);
XmlSerializer _xmlSerializer = new XmlSerializer(typeof(type));
FileStream _docStream = new FileStream(path, FileMode.Open, FileAccess.Read);
using (XmlReader _writer = XmlReader.Create(_docStream))
_content = (type)_xmlSerializer.Deserialize(_writer);
return _content;
}
#endregion

}
}
50 changes: 50 additions & 0 deletions Common/Infrastructure/UAOOI.Common.Infrastructure.Loc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e71824a

Please sign in to comment.