Skip to content

Commit

Permalink
Update to XmiTranspilerExtension to write to files
Browse files Browse the repository at this point in the history
 - Allow for overwriting files using the `XmiTranspilerExtension.WriteToFile` method
 - Added comments
 - Added pre-emptive support for v2.2 of the standard
  • Loading branch information
tbm0115 committed Feb 16, 2023
1 parent 2838a74 commit 3a36367
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
12 changes: 12 additions & 0 deletions MtconnectTranspiler/Contracts/IXmiElement.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
namespace MtconnectTranspiler.Contracts
{
/// <summary>
/// Generic reference to a <c>xmi</c> element
/// </summary>
public interface IXmiElement
{
/// <summary>
/// Represents the <c>xmi:id</c> attribute on the element
/// </summary>
string Id { get; set; }

/// <summary>
/// Represents the <c>name</c> attribute on the element
/// </summary>
string Name { get; set; }

/// <summary>
/// Represents the <c>xmi:type</c> attribute on the element
/// </summary>
string Type { get; set; }
}
}
4 changes: 4 additions & 0 deletions MtconnectTranspiler/Contracts/MTConnectVersions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,9 @@ public enum MTConnectVersions
/// Refers to Version 2.1 of MTConnect
/// </summary>
v2_1,
/// <summary>
/// Refers to Version 2.2 of MTConnect
/// </summary>
v2_2
}
}
22 changes: 21 additions & 1 deletion MtconnectTranspiler/Contracts/XmiDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@

namespace MtconnectTranspiler.Contracts
{
/// <summary>
/// A class that can deserialize a XMI document into an object-oriented form.
/// </summary>
public sealed class XmiDeserializer
{
private ILogger<XmiDeserializer>? _logger;
private XmlDocument xDoc;
private XmlNamespaceManager nsmgr;

/// <summary>
/// Constructs a new instance of the deserializer with a reference to the source document.
/// </summary>
/// <param name="xmlDocument">A source of XMI to deserialize</param>
/// <param name="logger"><inheritdoc cref="ILogger" path="/summary"/></param>
public XmiDeserializer(XmlDocument xmlDocument, ILogger<XmiDeserializer>? logger = null)
{
_logger = logger;
Expand All @@ -41,7 +49,7 @@ public XmiDeserializer(XmlDocument xmlDocument, ILogger<XmiDeserializer>? logger
/// </summary>
/// <typeparam name="T">Generic type to deserialize the XML document into.</typeparam>
/// <param name="predicatePath">Predicate XPath to start deserializing from.</param>
/// <returns></returns>
/// <returns>The deserialized object as <typeparamref name="T"/>.</returns>
public T? Deserialize<T>(string predicatePath, CancellationToken cancellationToken) where T : class, new()
{
XmlNode? xPredicate = xDoc.SelectSingleNode(predicatePath, nsmgr);
Expand Down Expand Up @@ -138,6 +146,12 @@ public XmiDeserializer(XmlDocument xmlDocument, ILogger<XmiDeserializer>? logger
return result;
}

/// <summary>
/// Creates a <see cref="XmiDeserializer"/> from a reference to the filepath of a XMI document.
/// </summary>
/// <param name="filename">Filepath to a XMI-formatted XML document.</param>
/// <param name="logger"><inheritdoc cref="ILogger" path="/summary"/></param>
/// <returns><inheritdoc cref="XmiDeserializer" path="/summary"/></returns>
public static XmiDeserializer FromFile(string filename, ILogger<XmiDeserializer>? logger = null)
{
var xDoc = new XmlDocument();
Expand All @@ -146,6 +160,12 @@ public static XmiDeserializer FromFile(string filename, ILogger<XmiDeserializer>
return new XmiDeserializer(xDoc, logger);
}

/// <summary>
/// Creates a <see cref="XmiDeserializer"/> from raw XML.
/// </summary>
/// <param name="xml">Raw XML string</param>
/// <param name="logger"><inheritdoc cref="ILogger" path="/summary"/></param>
/// <returns><inheritdoc cref="XmiDeserializer" path="/summary"/></returns>
public static XmiDeserializer FromXml(string xml, ILogger<XmiDeserializer>? logger = null)
{
var xDoc = new XmlDocument();
Expand Down
2 changes: 1 addition & 1 deletion MtconnectTranspiler/TranspilerDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class TranspilerDispatcher : IDisposable
/// </summary>
public TranspilerDispatcherOptions Options { get; private set; }

public TranspilerDispatcher(TranspilerDispatcherOptions options, ILogger<TranspilerDispatcher>? logger = null)
public TranspilerDispatcher(TranspilerDispatcherOptions options, ILogger<TranspilerDispatcher>? logger = default)
{
_logger = logger;
Options = options;
Expand Down
22 changes: 13 additions & 9 deletions MtconnectTranspiler/XmiTranspilerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,25 @@ public static class XmiTranspilerExtensions
/// </summary>
/// <param name="filepath">Location to write the contents</param>
/// <param name="contents"></param>
public static void WriteToFile(string filepath, string contents)
public static void WriteToFile(string filepath, string contents, bool overwriteExisting = false)
{
string folder = Path.GetDirectoryName(filepath);
if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);

int copyCount = 0;
do
if (!overwriteExisting)
{
if (File.Exists(filepath))
int copyCount = 0;
do
{
copyCount += 1;
int extensionIndex = filepath.LastIndexOf(".");
filepath = $"{filepath.Substring(0, extensionIndex)}_{copyCount}{filepath.Substring(extensionIndex)}";
}
} while (File.Exists(filepath));
if (File.Exists(filepath))
{
copyCount += 1;
int extensionIndex = filepath.LastIndexOf(".");
filepath = $"{filepath.Substring(0, extensionIndex)}_{copyCount}{filepath.Substring(extensionIndex)}";
}
} while (File.Exists(filepath));
}

File.WriteAllText(filepath, contents);
}
}
Expand Down

0 comments on commit 3a36367

Please sign in to comment.