From 572f0bafcafb2dc2a46710facc383f704a72624b Mon Sep 17 00:00:00 2001 From: Mariusz Date: Wed, 23 Jun 2021 22:17:08 +0200 Subject: [PATCH] Use Common XML serializer to manage xml documents #228 - fixed tests - work in progress - UT :+1: --- .../ProjectConfigurationManagementUnitTest.cs | 5 +- .../IO/ProjectConfigurationManagement.cs | 19 ++- .../IO/SolutionConfigurationManagement.cs | 27 ++-- .../IO/TypeGenericConfigurationManagement.cs | 10 +- .../Solution/UAModelDesignerSolution.cs | 19 ++- .../IStylesheetNameProvider.cs | 20 +-- ModelDesigner.ImportExport/XmlFile.cs | 138 +++++++++--------- 7 files changed, 135 insertions(+), 103 deletions(-) diff --git a/ModelDesigner.DesignStudio.UnitTest/IO/ProjectConfigurationManagementUnitTest.cs b/ModelDesigner.DesignStudio.UnitTest/IO/ProjectConfigurationManagementUnitTest.cs index 342a5bc9..0dd598fa 100644 --- a/ModelDesigner.DesignStudio.UnitTest/IO/ProjectConfigurationManagementUnitTest.cs +++ b/ModelDesigner.DesignStudio.UnitTest/IO/ProjectConfigurationManagementUnitTest.cs @@ -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); diff --git a/ModelDesigner.DesignStudio/IO/ProjectConfigurationManagement.cs b/ModelDesigner.DesignStudio/IO/ProjectConfigurationManagement.cs index 16b87404..4341e754 100644 --- a/ModelDesigner.DesignStudio/IO/ProjectConfigurationManagement.cs +++ b/ModelDesigner.DesignStudio/IO/ProjectConfigurationManagement.cs @@ -62,15 +62,22 @@ private static string ReplaceTokenAndReturnFullPath(string fileNameToBeProcessed #region TypeGenericConfigurationManagement - protected override XmlFile.DataToSerialize PrepareDataToSerialize(OpcUaModelCompiler.ModelDesign modelDesign) + protected override string PrepareDataToSerialize() { - XmlFile.DataToSerialize _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 PrepareDataToSerialize(OpcUaModelCompiler.ModelDesign modelDesign) + //{ + // XmlFile.DataToSerialize _config; + // _config.Data = modelDesign; + // _config.XmlNamespaces = null; + // _config.StylesheetName = "OPCUAModelDesign.xslt"; + // return _config; + //} + #endregion TypeGenericConfigurationManagement #region constructor diff --git a/ModelDesigner.DesignStudio/IO/SolutionConfigurationManagement.cs b/ModelDesigner.DesignStudio/IO/SolutionConfigurationManagement.cs index acd00f52..527e21a8 100644 --- a/ModelDesigner.DesignStudio/IO/SolutionConfigurationManagement.cs +++ b/ModelDesigner.DesignStudio/IO/SolutionConfigurationManagement.cs @@ -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; @@ -49,15 +48,21 @@ internal static void SetupFileDialog(IFileDialog dialog) #region TypeGenericConfigurationManagement - protected override XmlFile.DataToSerialize PrepareDataToSerialize(UAModelDesignerSolution solutionDesription) + protected override string PrepareDataToSerialize() { - XmlFile.DataToSerialize _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 PrepareDataToSerialize(UAModelDesignerSolution solutionDesription) + //{ + // XmlFile.DataToSerialize _config; + // _config.Data = solutionDesription; + // _config.XmlNamespaces = null; + // _config.StylesheetName = "UAModelDesignerSolution.xslt"; + // return _config; + //} + #endregion TypeGenericConfigurationManagement #region ISolutionConfigurationManagement diff --git a/ModelDesigner.DesignStudio/IO/TypeGenericConfigurationManagement.cs b/ModelDesigner.DesignStudio/IO/TypeGenericConfigurationManagement.cs index e9b298e9..0474a79c 100644 --- a/ModelDesigner.DesignStudio/IO/TypeGenericConfigurationManagement.cs +++ b/ModelDesigner.DesignStudio/IO/TypeGenericConfigurationManagement.cs @@ -19,7 +19,7 @@ namespace CAS.UA.Model.Designer.IO /// The type of the associated node configuration to be serialized. /// internal abstract class TypeGenericConfigurationManagement : ConfigurationManagement - where Type4Serialization : class, new() + where Type4Serialization : class, UAOOI.Common.Infrastructure.Serializers.INamespaces, new() { #region constructors @@ -74,10 +74,10 @@ protected void Save(Type4Serialization modelDesign) { try { - XmlFile.DataToSerialize dataToSerialize = PrepareDataToSerialize(modelDesign); + string stylesheetName = PrepareDataToSerialize(); GraphicalUserInterface.UseWaitCursor = true; BeforeWrite?.Invoke(this, new StringEventArgs(DefaultFileName)); - XmlFile.WriteXmlFile(dataToSerialize.Data, DefaultFileName, FileMode.Create, dataToSerialize.StylesheetName, dataToSerialize.XmlNamespaces); + XmlFile.WriteXmlFile(modelDesign, DefaultFileName, FileMode.Create, stylesheetName); ChangesArePresent = false; } catch (Exception ex) @@ -89,8 +89,8 @@ protected void Save(Type4Serialization modelDesign) GraphicalUserInterface.UseWaitCursor = false; } } - - protected abstract XmlFile.DataToSerialize PrepareDataToSerialize(Type4Serialization modelDesign); + //TODO Use Common XML serializer to manage xml documents #228 + protected abstract string PrepareDataToSerialize(); /// /// Reads the configuration. diff --git a/ModelDesigner.DesignStudio/Solution/UAModelDesignerSolution.cs b/ModelDesigner.DesignStudio/Solution/UAModelDesignerSolution.cs index 0fd9dfd5..c0b55f70 100644 --- a/ModelDesigner.DesignStudio/Solution/UAModelDesignerSolution.cs +++ b/ModelDesigner.DesignStudio/Solution/UAModelDesignerSolution.cs @@ -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 { /// /// Class UAModelDesignerSolution. /// - public partial class UAModelDesignerSolution + public partial class UAModelDesignerSolution : UAOOI.Common.Infrastructure.Serializers.INamespaces { /// /// Creates an empty solution model. @@ -31,6 +33,12 @@ internal static UAModelDesignerSolution CreateEmptyModel(string solutionName) }; } + public IEnumerable GetNamespaces() + { + //TODO Use Common XML serializer to manage xml documents #228 + return new List(); + } + [OnDeserialized()] public void OnDeserialized(StreamingContext context) { @@ -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) diff --git a/ModelDesigner.ImportExport/IStylesheetNameProvider.cs b/ModelDesigner.ImportExport/IStylesheetNameProvider.cs index 08a255ec..92f27fbf 100644 --- a/ModelDesigner.ImportExport/IStylesheetNameProvider.cs +++ b/ModelDesigner.ImportExport/IStylesheetNameProvider.cs @@ -16,15 +16,15 @@ namespace CAS.UA.Model.Designer.ImportExport { - /// - /// Represents XML file style sheet name provider - /// - public interface IStylesheetNameProvider - { - /// - /// The style sheet name - /// - string StylesheetName { get; } + ///// + ///// Represents XML file style sheet name provider + ///// + //public interface IStylesheetNameProvider + //{ + // /// + // /// The style sheet name + // /// + // string StylesheetName { get; } - } + //} } diff --git a/ModelDesigner.ImportExport/XmlFile.cs b/ModelDesigner.ImportExport/XmlFile.cs index c76438d1..32bfefea 100644 --- a/ModelDesigner.ImportExport/XmlFile.cs +++ b/ModelDesigner.ImportExport/XmlFile.cs @@ -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 { @@ -22,13 +24,9 @@ public static class XmlFile /// /// A structure containing dat to be serialized /// - public struct DataToSerialize + public struct DataToSerialize : IStylesheetNameProvider, INamespaces + where Type4Serialization: INamespaces { - /// - /// The referenced by the object. - /// - public XmlSerializerNamespaces XmlNamespaces; - /// /// The object containing working data to be serialized and saved in the file. /// @@ -37,35 +35,45 @@ public struct DataToSerialize /// ///Name of the stylesheet document. /// - public string StylesheetName; - } + public string StylesheetName { get; internal set; } - /// - /// Serializes the specified and writes the XML document to a file. - /// - /// type of the root object to be serialized and saved in the file. - /// The structure containing working data to be serialized and saved in the file. - /// A relative or absolute path for the file containing the serialized object. - /// Specifies how the operating system should open a file . - public static void WriteXmlFile(DataToSerialize dataObject, string path, FileMode mode) - { - WriteXmlFile(dataObject.Data, path, mode, dataObject.StylesheetName, dataObject.XmlNamespaces); - } + #region INamespaces - /// - /// Serializes the specified and writes the XML document to a file. - /// - /// The type of the root object to be serialized and saved in the file. - /// The object containing working data to be serialized and saved in the file. - /// A relative or absolute path for the file containing the serialized object. - /// Specifies how the operating system should open a file . - /// Name of the stylesheet document. - /// or stylesheetName - public static void WriteXmlFile(type dataObject, string path, FileMode mode, string stylesheetName) - { - WriteXmlFile(dataObject, path, mode, stylesheetName, null); + public IEnumerable GetNamespaces() + { + throw new NotImplementedException(); + } + + #endregion INamespaces } + ///// + ///// Serializes the specified and writes the XML document to a file. + ///// + ///// type of the root object to be serialized and saved in the file. + ///// The structure containing working data to be serialized and saved in the file. + ///// A relative or absolute path for the file containing the serialized object. + ///// Specifies how the operating system should open a file . + //public static void WriteXmlFile(type dataObject, string path, FileMode mode, string stylesheetName) + //{ + // UAOOI.Common.Infrastructure.Serializers.XmlFile.WriteXmlFile(dataObject, path, mode, ); + // //WriteXmlFile(dataObject.Data, path, mode, dataObject.StylesheetName, dataObject.XmlNamespaces); + //} + + ///// + ///// Serializes the specified and writes the XML document to a file. + ///// + ///// The type of the root object to be serialized and saved in the file. + ///// The object containing working data to be serialized and saved in the file. + ///// A relative or absolute path for the file containing the serialized object. + ///// Specifies how the operating system should open a file . + ///// Name of the stylesheet document. + ///// or stylesheetName + //public static void WriteXmlFile(type dataObject, string path, FileMode mode, string stylesheetName) + //{ + // WriteXmlFile(dataObject, path, mode, stylesheetName, null); + //} + /// /// Serializes the specified and writes the XML document to a file. /// @@ -76,43 +84,43 @@ public static void WriteXmlFile(type dataObject, string path, FileMode mod /// Name of the stylesheet document. /// The referenced by the object. /// or stylesheetName - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")] - public static void WriteXmlFile(type dataObject, string path, FileMode mode, string stylesheetName, XmlSerializerNamespaces xmlNamespaces) + public static void WriteXmlFile(type dataObject, string path, FileMode mode, string stylesheetName) + where type : INamespaces { - UAOOI.Common.Infrastructure.Serializers.XmlFile.WriteXmlFile(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(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); + //} } - /// - /// Serializes the specified and writes the XML document to a file. - /// - /// The type of the object to be serialized and saved in the file. - /// The object containing working data to be serialized and saved in the file. - /// A relative or absolute path for the file containing the serialized object. - /// Specifies how the operating system should open a file. - public static void WriteXmlFile(type dataObject, string path, FileMode mode) - where type : IStylesheetNameProvider - { - WriteXmlFile(dataObject, path, mode, dataObject.StylesheetName); - } + ///// + ///// Serializes the specified and writes the XML document to a file. + ///// + ///// The type of the object to be serialized and saved in the file. + ///// The object containing working data to be serialized and saved in the file. + ///// A relative or absolute path for the file containing the serialized object. + ///// Specifies how the operating system should open a file. + //public static void WriteXmlFile(type dataObject, string path, FileMode mode) + // where type : IStylesheetNameProvider + //{ + // WriteXmlFile(dataObject, path, mode, dataObject.StylesheetName); + //} /// /// Reads an XML document from the file and deserializes its content to returned object.