Skip to content

Commit

Permalink
Use Common XML serializer to manage xml documents #228
Browse files Browse the repository at this point in the history
- fixed tests
- work in progress
- UT 👍
  • Loading branch information
mpostol committed Jun 23, 2021
1 parent 3a4d4b6 commit 572f0ba
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ public void SaveNewTest()
Assert.IsNotNull(_newItem.UAModelDesignerProject);
_solutionMock.Verify(x => x.DefaultDirectory, Times.AtLeastOnce);
_solutionMock.Verify(x => x.DefaultFileName, Times.Never);
ModelDesign _modelDesign = new ModelDesign();
ModelDesign _modelDesign = new ModelDesign
{
Namespaces = new Namespace[] { new Namespace() { Name = "Namespace" } }
};
//test save
_newItem.Save(_modelDesign);
_solutionMock.Verify(x => x.DefaultDirectory, Times.AtLeastOnce);
Expand Down
19 changes: 13 additions & 6 deletions ModelDesigner.DesignStudio/IO/ProjectConfigurationManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,22 @@ private static string ReplaceTokenAndReturnFullPath(string fileNameToBeProcessed

#region TypeGenericConfigurationManagement<OpcUaModelCompiler.ModelDesign>

protected override XmlFile.DataToSerialize<OpcUaModelCompiler.ModelDesign> PrepareDataToSerialize(OpcUaModelCompiler.ModelDesign modelDesign)
protected override string PrepareDataToSerialize()
{
XmlFile.DataToSerialize<OpcUaModelCompiler.ModelDesign> _config;
_config.Data = modelDesign;
_config.XmlNamespaces = null;
_config.StylesheetName = "OPCUAModelDesign.xslt";
return _config;
return String.Empty;
}

//TODO Use Common XML serializer to manage xml documents #228

//protected override XmlFile.DataToSerialize<OpcUaModelCompiler.ModelDesign> PrepareDataToSerialize(OpcUaModelCompiler.ModelDesign modelDesign)
//{
// XmlFile.DataToSerialize<OpcUaModelCompiler.ModelDesign> _config;
// _config.Data = modelDesign;
// _config.XmlNamespaces = null;
// _config.StylesheetName = "OPCUAModelDesign.xslt";
// return _config;
//}

#endregion TypeGenericConfigurationManagement<OpcUaModelCompiler.ModelDesign>

#region constructor
Expand Down
27 changes: 16 additions & 11 deletions ModelDesigner.DesignStudio/IO/SolutionConfigurationManagement.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
//___________________________________________________________________________________
//__________________________________________________________________________________________________
//
// Copyright (C) 2020, Mariusz Postol LODZ POLAND.
// Copyright (C) 2021 Mariusz Postol LODZ POLAND.
//
// To be in touch join the community at GITTER: https://gitter.im/mpostol/OPC-UA-OOI
//___________________________________________________________________________________
// To be in touch join the community at GitHub: https://github.com/mpostol/OPC-UA-OOI/discussions
//__________________________________________________________________________________________________

using CAS.CommServer.UA.ModelDesigner.Configuration;
using CAS.CommServer.UA.ModelDesigner.Configuration.IO;
using CAS.CommServer.UA.ModelDesigner.Configuration.UserInterface;
using CAS.UA.Model.Designer.ImportExport;
using CAS.UA.Model.Designer.Properties;
using CAS.UA.Model.Designer.Solution;
using System;
Expand Down Expand Up @@ -49,15 +48,21 @@ internal static void SetupFileDialog(IFileDialog dialog)

#region TypeGenericConfigurationManagement

protected override XmlFile.DataToSerialize<UAModelDesignerSolution> PrepareDataToSerialize(UAModelDesignerSolution solutionDesription)
protected override string PrepareDataToSerialize()
{
XmlFile.DataToSerialize<UAModelDesignerSolution> _config;
_config.Data = solutionDesription;
_config.XmlNamespaces = null;
_config.StylesheetName = "UAModelDesignerSolution.xslt";
return _config;
return "UAModelDesignerSolution.xslt";
}

//TODO Use Common XML serializer to manage xml documents #228
//protected override XmlFile.DataToSerialize<UAModelDesignerSolution> PrepareDataToSerialize(UAModelDesignerSolution solutionDesription)
//{
// XmlFile.DataToSerialize<UAModelDesignerSolution> _config;
// _config.Data = solutionDesription;
// _config.XmlNamespaces = null;
// _config.StylesheetName = "UAModelDesignerSolution.xslt";
// return _config;
//}

#endregion TypeGenericConfigurationManagement

#region ISolutionConfigurationManagement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace CAS.UA.Model.Designer.IO
/// <typeparam name="Type4Serialization">The type of the associated node configuration to be serialized.</typeparam>
/// <seealso cref="ConfigurationManagement" />
internal abstract class TypeGenericConfigurationManagement<Type4Serialization> : ConfigurationManagement
where Type4Serialization : class, new()
where Type4Serialization : class, UAOOI.Common.Infrastructure.Serializers.INamespaces, new()
{
#region constructors

Expand Down Expand Up @@ -74,10 +74,10 @@ protected void Save(Type4Serialization modelDesign)
{
try
{
XmlFile.DataToSerialize<Type4Serialization> dataToSerialize = PrepareDataToSerialize(modelDesign);
string stylesheetName = PrepareDataToSerialize();
GraphicalUserInterface.UseWaitCursor = true;
BeforeWrite?.Invoke(this, new StringEventArgs(DefaultFileName));
XmlFile.WriteXmlFile<Type4Serialization>(dataToSerialize.Data, DefaultFileName, FileMode.Create, dataToSerialize.StylesheetName, dataToSerialize.XmlNamespaces);
XmlFile.WriteXmlFile<Type4Serialization>(modelDesign, DefaultFileName, FileMode.Create, stylesheetName);
ChangesArePresent = false;
}
catch (Exception ex)
Expand All @@ -89,8 +89,8 @@ protected void Save(Type4Serialization modelDesign)
GraphicalUserInterface.UseWaitCursor = false;
}
}

protected abstract XmlFile.DataToSerialize<Type4Serialization> PrepareDataToSerialize(Type4Serialization modelDesign);
//TODO Use Common XML serializer to manage xml documents #228
protected abstract string PrepareDataToSerialize();

/// <summary>
/// Reads the configuration.
Expand Down
19 changes: 14 additions & 5 deletions ModelDesigner.DesignStudio/Solution/UAModelDesignerSolution.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
//___________________________________________________________________________________
//__________________________________________________________________________________________________
//
// Copyright (C) 2020, Mariusz Postol LODZ POLAND.
// Copyright (C) 2021, Mariusz Postol LODZ POLAND.
//
// To be in touch join the community at GITTER: https://gitter.im/mpostol/OPC-UA-OOI
//___________________________________________________________________________________
// To be in touch join the community at GitHub: https://github.com/mpostol/OPC-UA-OOI/discussions
//__________________________________________________________________________________________________

using CAS.UA.Model.Designer.Properties;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;

namespace CAS.UA.Model.Designer.Solution
{
/// <summary>
/// Class UAModelDesignerSolution.
/// </summary>
public partial class UAModelDesignerSolution
public partial class UAModelDesignerSolution : UAOOI.Common.Infrastructure.Serializers.INamespaces
{
/// <summary>
/// Creates an empty solution model.
Expand All @@ -31,6 +33,12 @@ internal static UAModelDesignerSolution CreateEmptyModel(string solutionName)
};
}

public IEnumerable<XmlQualifiedName> GetNamespaces()
{
//TODO Use Common XML serializer to manage xml documents #228
return new List<XmlQualifiedName>();
}

[OnDeserialized()]
public void OnDeserialized(StreamingContext context)
{
Expand All @@ -48,6 +56,7 @@ internal static UAModelDesignerSolutionServerDetails CreateEmptyInstance()
return new UAModelDesignerSolutionServerDetails() { codebase = string.Empty, configuration = string.Empty };
}
}

public partial class UAModelDesignerProject
{
internal static UAModelDesignerProject CreateEmpty(string name)
Expand Down
20 changes: 10 additions & 10 deletions ModelDesigner.ImportExport/IStylesheetNameProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
namespace CAS.UA.Model.Designer.ImportExport
{

/// <summary>
/// Represents XML file style sheet name provider
/// </summary>
public interface IStylesheetNameProvider
{
/// <summary>
/// The style sheet name
/// </summary>
string StylesheetName { get; }
///// <summary>
///// Represents XML file style sheet name provider
///// </summary>
//public interface IStylesheetNameProvider
//{
// /// <summary>
// /// The style sheet name
// /// </summary>
// string StylesheetName { get; }

}
//}
}
138 changes: 73 additions & 65 deletions ModelDesigner.ImportExport/XmlFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
//__________________________________________________________________________________________________

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using UAOOI.Common.Infrastructure.Serializers;

namespace CAS.UA.Model.Designer.ImportExport
{
Expand All @@ -22,13 +24,9 @@ public static class XmlFile
/// <summary>
/// A structure containing dat to be serialized
/// </summary>
public struct DataToSerialize<Type4Serialization>
public struct DataToSerialize<Type4Serialization> : IStylesheetNameProvider, INamespaces
where Type4Serialization: INamespaces
{
/// <summary>
/// The <see cref="XmlSerializerNamespaces"/> referenced by the object.
/// </summary>
public XmlSerializerNamespaces XmlNamespaces;

/// <summary>
/// The object containing working data to be serialized and saved in the file.
/// </summary>
Expand All @@ -37,35 +35,45 @@ public struct DataToSerialize<Type4Serialization>
/// <summary>
///Name of the stylesheet document.
/// </summary>
public string StylesheetName;
}
public string StylesheetName { get; internal set; }

/// <summary>
/// Serializes the specified <paramref name="dataObject" /> and writes the XML document to a file.
/// </summary>
/// <typeparam name="type"> type of the root object to be serialized and saved in the file.</typeparam>
/// <param name="dataObject">The structure 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>
public static void WriteXmlFile<type>(DataToSerialize<type> dataObject, string path, FileMode mode)
{
WriteXmlFile<type>(dataObject.Data, path, mode, dataObject.StylesheetName, dataObject.XmlNamespaces);
}
#region INamespaces

/// <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"><paramref name="path"/> or <paramref name="dataObject"/> stylesheetName</exception>
public static void WriteXmlFile<type>(type dataObject, string path, FileMode mode, string stylesheetName)
{
WriteXmlFile<type>(dataObject, path, mode, stylesheetName, null);
public IEnumerable<XmlQualifiedName> GetNamespaces()
{
throw new NotImplementedException();
}

#endregion INamespaces
}

///// <summary>
///// Serializes the specified <paramref name="dataObject" /> and writes the XML document to a file.
///// </summary>
///// <typeparam name="type"> type of the root object to be serialized and saved in the file.</typeparam>
///// <param name="dataObject">The structure 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>
//public static void WriteXmlFile<type>(type dataObject, string path, FileMode mode, string stylesheetName)
//{
// UAOOI.Common.Infrastructure.Serializers.XmlFile.WriteXmlFile<type>(dataObject, path, mode, );
// //WriteXmlFile<type>(dataObject.Data, path, mode, dataObject.StylesheetName, dataObject.XmlNamespaces);
//}

///// <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"><paramref name="path"/> or <paramref name="dataObject"/> stylesheetName</exception>
//public static void WriteXmlFile<type>(type dataObject, string path, FileMode mode, string stylesheetName)
//{
// WriteXmlFile<type>(dataObject, path, mode, stylesheetName, null);
//}

/// <summary>
/// Serializes the specified <paramref name="dataObject" /> and writes the XML document to a file.
/// </summary>
Expand All @@ -76,43 +84,43 @@ public static void WriteXmlFile<type>(type dataObject, string path, FileMode mod
/// <param name="stylesheetName">Name of the stylesheet document.</param>
/// <param name="xmlNamespaces">The <see cref="System.Xml.Serialization.XmlSerializerNamespaces"/> referenced by the object.</param>
/// <exception cref="System.ArgumentNullException"><paramref name="path"/> or <paramref name="dataObject"/> stylesheetName</exception>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
public static void WriteXmlFile<type>(type dataObject, string path, FileMode mode, string stylesheetName, XmlSerializerNamespaces xmlNamespaces)
public static void WriteXmlFile<type>(type dataObject, string path, FileMode mode, string stylesheetName)
where type : INamespaces
{
UAOOI.Common.Infrastructure.Serializers.XmlFile.WriteXmlFile<type>(dataObject, path, mode);
if (string.IsNullOrEmpty(path))
throw new ArgumentNullException("path");
if (dataObject == null)
throw new ArgumentNullException("content");
//TODO Use Common XML serializer to manage xml documents #228
XmlSerializer _srlzr = new XmlSerializer(typeof(type));
XmlWriterSettings _setting = new XmlWriterSettings()
{
Indent = true,
IndentChars = " ",
NewLineChars = "\r\n"
};
using (FileStream _docStrm = new FileStream(path, mode, FileAccess.Write))
using (XmlWriter _writer = XmlWriter.Create(_docStrm, _setting))
{
if (!string.IsNullOrEmpty(stylesheetName))
_writer.WriteProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" " + string.Format("href=\"{0}\"", stylesheetName));
_srlzr.Serialize(_writer, dataObject, xmlNamespaces);
}
UAOOI.Common.Infrastructure.Serializers.XmlFile.WriteXmlFile<type>(dataObject, path, mode, stylesheetName);
//if (string.IsNullOrEmpty(path))
// throw new ArgumentNullException("path");
//if (dataObject == null)
// throw new ArgumentNullException("content");
////TODO Use Common XML serializer to manage xml documents #228
//XmlSerializer _srlzr = new XmlSerializer(typeof(type));
//XmlWriterSettings _setting = new XmlWriterSettings()
//{
// Indent = true,
// IndentChars = " ",
// NewLineChars = "\r\n"
//};
//using (FileStream _docStrm = new FileStream(path, mode, FileAccess.Write))
//using (XmlWriter _writer = XmlWriter.Create(_docStrm, _setting))
//{
// if (!string.IsNullOrEmpty(stylesheetName))
// _writer.WriteProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" " + string.Format("href=\"{0}\"", stylesheetName));
// _srlzr.Serialize(_writer, dataObject, xmlNamespaces);
//}
}

/// <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
{
WriteXmlFile<type>(dataObject, path, mode, dataObject.StylesheetName);
}
///// <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
//{
// 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.
Expand Down

0 comments on commit 572f0ba

Please sign in to comment.