Skip to content

Commit

Permalink
Merge pull request #229 from mpostol/Development4.5-Charlie
Browse files Browse the repository at this point in the history
Use Common XML serializer to manage xml documents #228
  • Loading branch information
mpostol authored Jun 12, 2021
2 parents 633bd49 + 83631c2 commit 669048b
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 62 deletions.
72 changes: 37 additions & 35 deletions ModelDesigner.DesignStudio/HelpContent/HelpContentHelper.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
//<summary>
// Title : Helper to generate URL for documentation
// System : Microsoft Visual C# .NET 2008
// $LastChangedDate$
// $Rev$
// $LastChangedBy$
// $URL$
// $Id$
//__________________________________________________________________________________________________
//
// Copyright (C)2008, CAS LODZ POLAND.
// TEL: +48 (42) 686 25 47
// mailto://techsupp@cas.eu
// http://www.cas.eu
//</summary>
// Copyright (C) 2021, Mariusz Postol LODZ POLAND.
//
// To be in touch join the community at GitHub: https://github.com/mpostol/OPC-UA-OOI/discussions
//__________________________________________________________________________________________________

using CAS.MAML.HelpTopics.Content;
using System;
Expand All @@ -21,92 +13,102 @@

namespace CAS.UA.Model.Designer.HelpContent
{
class HelpContentHelper
internal class HelpContentHelper
{
#region private

private static SortedDictionary<string, TopicNode> urls;

/// <summary>
/// Initializes the <see cref="HelpContentHelper"/> class.
/// </summary>
static HelpContentHelper()
{
if ( !System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable() )
if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
return;
System.Net.WebClient Client = new System.Net.WebClient();
Stream stream;
try
{
using ( stream = Client.OpenRead( Properties.Settings.Default.HelpDocumentationAllTopicsWebAddress ) )
using (stream = Client.OpenRead(Properties.Settings.Default.HelpDocumentationAllTopicsWebAddress))
{
if ( stream == null )
if (stream == null)
return;
urls = Topics.FromXmlStream( stream );
urls = Topics.FromXmlStream(stream);
}
}
catch ( Exception ) { urls = null; }
if ( urls == null )
catch (Exception) { urls = null; }
if (urls == null)
urls = new SortedDictionary<string, TopicNode>();
}
#endregion

#endregion private

#region public

public enum SelectedTopicName
{
None,
Primary,
Secondary
}

/// <summary>
/// Gets the help URL.
/// </summary>
/// <param name="TopicName">Name of the topic.</param>
/// <returns>Complete Url to the proper site of help.</returns>
public static string GetHelpUrl( string PrimaryTopicName, string SecondaryTopicName, out SelectedTopicName selectedTopicName )
public static string GetHelpUrl(string PrimaryTopicName, string SecondaryTopicName, out SelectedTopicName selectedTopicName)
{
string ret = string.Empty;
selectedTopicName = SelectedTopicName.None;
if ( ( urls != null ) && ( urls.ContainsKey( PrimaryTopicName ) ) )
if ((urls != null) && (urls.ContainsKey(PrimaryTopicName)))
{
ret = Properties.Settings.Default.HelpDocumentationURLsuffix + urls[ PrimaryTopicName ].Url;
ret = Properties.Settings.Default.HelpDocumentationURLsuffix + urls[PrimaryTopicName].Url;
selectedTopicName = SelectedTopicName.Primary;
}
if ( ( urls != null ) && string.IsNullOrEmpty( ret ) && urls.ContainsKey( SecondaryTopicName ) )
if ((urls != null) && string.IsNullOrEmpty(ret) && urls.ContainsKey(SecondaryTopicName))
{
ret = Properties.Settings.Default.HelpDocumentationURLsuffix + urls[ SecondaryTopicName ].Url;
ret = Properties.Settings.Default.HelpDocumentationURLsuffix + urls[SecondaryTopicName].Url;
selectedTopicName = SelectedTopicName.Secondary;
}
return Properties.Settings.Default.HelpDocumentationOpcUaEbookURL + ret;
}

public static void Initialize()
{ }
#endregion

#endregion public

#region static

/// <summary>
/// Deserializes objects from XML file
/// </summary>
/// <param name="stream">The stream.</param>
/// <returns>Deserialized object</returns>
internal static SortedDictionary<string, TopicNode> FromXmlStream( Stream stream )
internal static SortedDictionary<string, TopicNode> FromXmlStream(Stream stream)
{
try
{
StreamReader reader = new StreamReader( stream );
StreamReader reader = new StreamReader(stream);
Topics tpcs = null;
using ( reader )
using (reader)
{
XmlSerializer xmlSerializer = new XmlSerializer( typeof( Topics ) );
tpcs = (Topics)xmlSerializer.Deserialize( reader );
//TODO Use Common XML serializer to manage xml documents #228
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Topics));
tpcs = (Topics)xmlSerializer.Deserialize(reader);
}
if ( tpcs == null )
if (tpcs == null)
return null;
return tpcs.CreateDictionary();
}
catch ( Exception )
catch (Exception)
{
return null;
}
}

#endregion static
}
}
}
9 changes: 5 additions & 4 deletions ModelDesigner.DesignStudio/Wrappers/WrapperBase.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//___________________________________________________________________________________
//__________________________________________________________________________________________________
//
// 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 System.IO;
using System.Xml.Serialization;
Expand Down Expand Up @@ -65,6 +65,7 @@ protected string ModelDesignerNodeStringRepresentation
get
{
StringWriter sw = new System.IO.StringWriter();
//TODO Use Common XML serializer to manage xml documents #228
XmlSerializer serializer = new XmlSerializer(this.ModelDesignerNode.GetType());
serializer.Serialize(sw, this.ModelDesignerNode);
return sw.ToString();
Expand Down
7 changes: 4 additions & 3 deletions ModelDesigner.DesignStudio/Wrappers/WrapperTreeNode.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//___________________________________________________________________________________
//__________________________________________________________________________________________________
//
// 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 CAS.UA.Model.Designer.ToForms;
Expand Down Expand Up @@ -86,6 +86,7 @@ private static object GetModelDesignerNodeFromStringRepresentation(string modelD
}
if (SerializedType == null)
return null;
//TODO Use Common XML serializer to manage xml documents #228
XmlSerializer serializer = new XmlSerializer(SerializedType);
stringReader = new StringReader(modelDesignerNodeStringRepresentation);
return serializer.Deserialize(stringReader);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//___________________________________________________________________________________
//__________________________________________________________________________________________________
//
// 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.UA.Model.Designer.Wrappers;
Expand All @@ -13,6 +13,7 @@ namespace CAS.UA.Model.Designer.Wrappers4ProperyGrid
{
/// <summary>
/// Instance of this class is to be used as a wrapper by the <see cref="PropertyGrid"/> to expose to the user and
//TODO Use Common XML serializer to manage xml documents #228
/// by the <see cref="XmlSerializer"/> to save information on the solution.
/// </summary>
[DefaultProperty("Server")]
Expand Down
1 change: 1 addition & 0 deletions ModelDesigner.ImportExport/MamlCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ private static bool WriteToXML( FileInfo filePathToSave, topic tpc )
{
using ( StreamWriter stWriter = new StreamWriter( filePathToSave.FullName ) )
{
//TODO Use Common XML serializer to manage xml documents #228
XmlSerializer xmlSerializer = new XmlSerializer( typeof( topic ) );
XmlSerializerNamespaces xs = new XmlSerializerNamespaces();
xs.Add( "", "http://ddue.schemas.microsoft.com/authoring/2003/5" );
Expand Down
33 changes: 17 additions & 16 deletions ModelDesigner.ImportExport/XmlFile.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
//_______________________________________________________________
// Title : XmlFile - Provides static methods for serialization objects into XML documents and writing the XML document to a file.
// System : Microsoft VisualStudio 2015 / C#
// $LastChangedDate$
// $Rev$
// $LastChangedBy$
// $URL$
// $Id$
//__________________________________________________________________________________________________
//
// Copyright (C) 2017, CAS LODZ POLAND.
// TEL: +48 608 61 98 99
// mailto://techsupp@cas.eu
// http://www.cas.eu
//_______________________________________________________________
// Copyright (C) 2021, Mariusz Postol LODZ POLAND.
//
// To be in touch join the community at GitHub: https://github.com/mpostol/OPC-UA-OOI/discussions
//__________________________________________________________________________________________________

using System;
using System.IO;
Expand All @@ -25,8 +17,8 @@ namespace CAS.UA.Model.Designer.ImportExport
/// </summary>
public static class XmlFile
{

#region public

/// <summary>
/// A structure containing dat to be serialized
/// </summary>
Expand All @@ -36,15 +28,18 @@ public struct DataToSerialize<Type4Serialization>
/// 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>
public Type4Serialization Data;

/// <summary>
///Name of the stylesheet document.
/// </summary>
public string StylesheetName;
}

/// <summary>
/// Serializes the specified <paramref name="dataObject" /> and writes the XML document to a file.
/// </summary>
Expand All @@ -56,6 +51,7 @@ public static void WriteXmlFile<type>(DataToSerialize<type> dataObject, string p
{
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>
Expand All @@ -69,6 +65,7 @@ public static void WriteXmlFile<type>(type dataObject, string path, FileMode mod
{
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 @@ -86,6 +83,7 @@ public static void WriteXmlFile<type>(type dataObject, string path, FileMode mod
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()
{
Expand All @@ -101,6 +99,7 @@ public static void WriteXmlFile<type>(type dataObject, string path, FileMode mod
_srlzr.Serialize(_writer, dataObject, xmlNamespaces);
}
}

/// <summary>
/// Serializes the specified <paramref name="dataObject"/> and writes the XML document to a file.
/// </summary>
Expand All @@ -113,6 +112,7 @@ public static void WriteXmlFile<type>(type dataObject, string path, FileMode mod
{
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>
Expand All @@ -126,13 +126,14 @@ public static type ReadXmlFile<type>(string path)
if (string.IsNullOrEmpty(path))
throw new ArgumentNullException("path");
type _content = default(type);
//TODO Use Common XML serializer to manage xml documents #228
XmlSerializer _srlzr = new XmlSerializer(typeof(type));
using (FileStream _docStrm = new FileStream(path, FileMode.Open, FileAccess.Read))
using (XmlReader _writer = XmlReader.Create(_docStrm))
_content = (type)_srlzr.Deserialize(_writer);
return _content;
}
#endregion

#endregion public
}
}
}

0 comments on commit 669048b

Please sign in to comment.