Skip to content

Commit

Permalink
Merge pull request #595 from mpostol/SemanticData630
Browse files Browse the repository at this point in the history
Use Common XML serializer to manage xml documents
  • Loading branch information
mpostol authored May 7, 2021
2 parents d55f8e5 + 2706dc8 commit 2170d04
Showing 8 changed files with 45 additions and 133 deletions.
22 changes: 16 additions & 6 deletions Common/Infrastructure/Serializers/XmlFile.cs
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ 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
public static class XmlFile
{

#region public
@@ -70,19 +70,29 @@ public static void WriteXmlFile<type>(type dataObject, string path, FileMode mod
/// <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>
/// <typeparam name="type">The type of the object to be deserialized.</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>
/// <exception cref="ArgumentNullException"> path is null or empty</exception>
public static type ReadXmlFile<type>(string path)
{
if (string.IsNullOrEmpty(path))
throw new ArgumentNullException(nameof(path));
StreamReader _docStream = new StreamReader(path);
return ReadXmlFile<type>(_docStream);
}
/// <summary>
/// Reads an XML document from the <paramref name="reader"/> and deserializes its content to returned object.
/// </summary>
/// <typeparam name="type">The type of the object to be deserialized.</typeparam>
/// <param name="reader">The source of the stream to be deserialized.</param>
/// <returns>An object of type <typeparamref name="type"/> containing working data retrieved from an XML stream..</returns>
public static type ReadXmlFile<type>(StreamReader reader)
{
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);
using (XmlReader xmlReader = XmlReader.Create(reader))
_content = (type)_xmlSerializer.Deserialize(xmlReader);
return _content;
}
#endregion
12 changes: 10 additions & 2 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.

14 changes: 2 additions & 12 deletions SemanticData/UAModelDesignExport.UnitTest/NodeSetUnitTest.cs
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@
using System.IO;
using System.Linq;
using System.Xml;
using UAOOI.Common.Infrastructure.Serializers;
using UAOOI.SemanticData.BuildingErrorsHandling;
using UAOOI.SemanticData.UAModelDesignExport.Instrumentation;
using UAOOI.SemanticData.UAModelDesignExport.XML;
@@ -21,21 +22,11 @@ namespace UAOOI.SemanticData.UAModelDesignExport
[DeploymentItem(@"Models\", @"Models\")]
public class NodeSetIntegrationTest
{
#region TestContext

private TestContext testContextInstance;

/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext
{
get => testContextInstance;
set => testContextInstance = value;
}

#endregion TestContext
//public TestContext TestContext { get; set; }

#region TestMethod

@@ -60,7 +51,6 @@ public void UAReferenceTestMethod()
Assert.IsTrue(_testDataFileInfo.Exists);
ModelDesign _expected = XmlFile.ReadXmlFile<ModelDesign>(@"Models\ReferenceTest.asp.xml");
List<TraceMessage> _trace = new List<TraceMessage>();
int _diagnosticCounter = 0;
string uri = "http://cas.eu/UA/CommServer/UnitTests/ReferenceTest";
using (TracedAddressSpaceContext addressSpace = new Instrumentation.TracedAddressSpaceContext())
{
Original file line number Diff line number Diff line change
@@ -113,6 +113,10 @@
<None Include="OPCUAOOIKey.snk" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Common\Infrastructure\Common.Infrastructure.csproj">
<Project>{8817a671-abb8-463a-a8a3-ddcfe781a6b6}</Project>
<Name>Common.Infrastructure</Name>
</ProjectReference>
<ProjectReference Include="..\BuildingErrorsHandling\SemanticData.BuildingErrorsHandling.csproj">
<Project>{9ca2f05b-fb18-49ad-8520-1bcc838f748c}</Project>
<Name>SemanticData.BuildingErrorsHandling</Name>
3 changes: 2 additions & 1 deletion SemanticData/UAModelDesignExport/ModelDesignExport.cs
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@

using System;
using System.IO;
using UAOOI.Common.Infrastructure.Serializers;
using UAOOI.SemanticData.BuildingErrorsHandling;
using UAOOI.SemanticData.InformationModelFactory;

@@ -51,7 +52,7 @@ public void ExportToXMLFile(string outputFilePtah, string stylesheetName)
if (String.IsNullOrEmpty(outputFilePtah))
throw new ArgumentNullException(nameof(outputFilePtah), $"{nameof(outputFilePtah)} must be a valid file path.");
XML.ModelDesign _model = m_Model.Export();
XML.XmlFile.WriteXmlFile<XML.ModelDesign>(_model, outputFilePtah, FileMode.Create, stylesheetName);
XmlFile.WriteXmlFile<XML.ModelDesign>(_model, outputFilePtah, FileMode.Create, stylesheetName);
m_traceEvent(TraceMessage.DiagnosticTraceMessage($"The ModelDesign XML has been saved to file {outputFilePtah} and decorated with the stylesheet {stylesheetName}"));
}
/// <summary>
Original file line number Diff line number Diff line change
@@ -53,6 +53,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Common\Infrastructure\Common.Infrastructure.csproj" />
<ProjectReference Include="..\BuildingErrorsHandling\SemanticData.BuildingErrorsHandling.csproj" />
<ProjectReference Include="..\InformationModelFactory\SemanticData.InformationModelFactory.csproj" />
</ItemGroup>
31 changes: 10 additions & 21 deletions SemanticData/UAModelDesignExport/XML/Resource.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
//___________________________________________________________________________________
//__________________________________________________________________________________________________
//
// Copyright (C) 2921, 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 System;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Xml.Serialization;
using UAOOI.Common.Infrastructure.Serializers;

namespace UAOOI.SemanticData.UAModelDesignExport.XML
{
@@ -23,30 +23,19 @@ public static class UAResources
/// </summary>
/// <returns>An instance of <see cref="ModelDesign"/> representing UA defined types</returns>
public static ModelDesign LoadUADefinedTypes()
{
return LoadResource<ModelDesign>(UADefinedTypesName);
}

private static string UADefinedTypesName => $"{typeof(UAResources).Namespace}.UA Defined Types.xml";

/// <summary>
/// Loads a schema from an embedded resource.
/// </summary>
private static type LoadResource<type>(string path)
{
try
{
Assembly assembly = Assembly.GetExecutingAssembly();
using (StreamReader reader = new StreamReader(assembly.GetManifestResourceStream(path)))
{
XmlSerializer serializer = new XmlSerializer(typeof(type));
return (type)serializer.Deserialize(reader);
}
using (StreamReader reader = new StreamReader(assembly.GetManifestResourceStream(UADefinedTypesName)))
return XmlFile.ReadXmlFile<ModelDesign>(reader);
}
catch (Exception e)
{
throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "Could not load resource '{0}' because the exception {1} reports the error {2}.", path, e.GetType().Name, e.Message), e);
throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "Could not load resource '{0}' because the exception {1} reports the error {2}.", UADefinedTypesName, e.GetType().Name, e.Message), e);
}
}

private static string UADefinedTypesName => $"{typeof(UAResources).Namespace}.UA Defined Types.xml";
}
}
91 changes: 0 additions & 91 deletions SemanticData/UAModelDesignExport/XML/XmlFile.cs

This file was deleted.

0 comments on commit 2170d04

Please sign in to comment.