From 79757f0079815c2ceb3b4d064783af0dcb4c4b10 Mon Sep 17 00:00:00 2001 From: Franziska Date: Mon, 31 Jul 2023 11:30:57 +0200 Subject: [PATCH 1/2] enable template helper to read formatversion G1.0a etc. --- EDILibrary/TemplateHelper.cs | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/EDILibrary/TemplateHelper.cs b/EDILibrary/TemplateHelper.cs index 3929d29..aa04168 100644 --- a/EDILibrary/TemplateHelper.cs +++ b/EDILibrary/TemplateHelper.cs @@ -2,8 +2,8 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text.RegularExpressions; using System.Xml.Linq; - using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -11,8 +11,10 @@ namespace EDILibrary { public class TemplateHelper { - // Todo @JoschaMetze: Add a docstring - protected static void ParseAPERAKString(string aperak, out string dataType, out int length, + + private const string FormatVersionPattern = "^(?[A-Z]{6,7})(?[A-Z]?\\d+\\.\\d+[a-z]?)$"; + + private static void ParseAPERAKString(string aperak, out string dataType, out int length, out List list) { dataType = null; @@ -44,8 +46,8 @@ protected static void ParseAPERAKString(string aperak, out string dataType, out } } - // Todo @JoschaMetze: Add a docstring - protected static void Recurse(XElement cur, JArray refObj, TreeElement tree) + + private static void Recurse(XElement cur, JArray refObj, TreeElement tree) { var i = cur.Descendants("field").Count(d => d.Parent == cur); var hasClass = cur.Descendants("class").Any(d => d.Parent == cur); @@ -442,17 +444,30 @@ public string ConvertFilesToJSON(string inputFileName, string outputFileName, st { tree = new GenericEDILoader().LoadTree(File.ReadAllText(treeFileName)); } - var tmp = new JArray(); Recurse(srcXml, tmp, tree); - //retriev version information from inputFileName - var part = inputFileName.Split(new[] { ".template" }, StringSplitOptions.None)[0].Split(System.IO.Path.DirectorySeparatorChar).Last(); - var version = part.Substring(part.IndexOf('.') - 1); + string version = RetrieveFormatVersionFromInputFileName(inputFileName); ((tmp[0] as JObject)["_meta"] as JObject).Add("version", version); File.WriteAllText(outputFileName, JsonConvert.SerializeObject(tmp)); return JsonConvert.SerializeObject(tmp); } + + private static string RetrieveFormatVersionFromInputFileName(string inputFileName) + { + var formatVersionRegex = new Regex(FormatVersionPattern); + var part = inputFileName.Split(new[] { ".template" }, StringSplitOptions.None)[0].Split(System.IO.Path.DirectorySeparatorChar).Last(); + var match = formatVersionRegex.Match(part); + if (match.Success) + { + return match.Groups["version"].Value; + } + else + { + throw new ArgumentException($"Format version could not be determined: {part}", nameof(inputFileName)); + } + } + // the format version will be written to the json template file, thus needs to be passed public string ConvertToJSON(string xmlTemplate, string treeTemplate, string formatVersion) { From 5f5a6c6d3f4998df8f0356d6961de5c005e343b9 Mon Sep 17 00:00:00 2001 From: konstantin Date: Mon, 31 Jul 2023 12:04:48 +0200 Subject: [PATCH 2/2] Add Tests for `RetrieveFormatVersionFromInputFileName` (#116) * Add Tests for `RetrieveFormatVersionFromInputFileName` * remve BOM --- EDILibrary/TemplateHelper.cs | 23 ++++++++++++++-------- EDILibraryTests/TemplateHelperTests.cs | 27 ++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 EDILibraryTests/TemplateHelperTests.cs diff --git a/EDILibrary/TemplateHelper.cs b/EDILibrary/TemplateHelper.cs index aa04168..9b0e2e2 100644 --- a/EDILibrary/TemplateHelper.cs +++ b/EDILibrary/TemplateHelper.cs @@ -12,7 +12,7 @@ namespace EDILibrary public class TemplateHelper { - private const string FormatVersionPattern = "^(?[A-Z]{6,7})(?[A-Z]?\\d+\\.\\d+[a-z]?)$"; + private static readonly Regex FormatVersionRegex = new(@"^(?[A-Z]{6,7})(?[A-Z]?\d+\.\d+[a-z]?)$"); private static void ParseAPERAKString(string aperak, out string dataType, out int length, out List list) @@ -138,7 +138,7 @@ private static void Recurse(XElement cur, JArray refObj, TreeElement tree) { meta.Add("sg", elem.Attribute("meta.sg").Value); } - //check for parent sg + //check for parent sg else if (cur.Attribute("ref") != null) { meta.Add("sg", cur.Attribute("ref").Value); @@ -453,19 +453,26 @@ public string ConvertFilesToJSON(string inputFileName, string outputFileName, st } - private static string RetrieveFormatVersionFromInputFileName(string inputFileName) + /// + /// Extract the Format version (e.g. "1.0a") from a given template file name (e.g. "COMDIS1.0a.template") + /// + /// path of name of the template file + /// + /// + public static string RetrieveFormatVersionFromInputFileName(string inputFileName) { - var formatVersionRegex = new Regex(FormatVersionPattern); - var part = inputFileName.Split(new[] { ".template" }, StringSplitOptions.None)[0].Split(System.IO.Path.DirectorySeparatorChar).Last(); - var match = formatVersionRegex.Match(part); + var actualFileName = inputFileName.Split(new[] { ".template" }, StringSplitOptions.None)[0].Split(Path.DirectorySeparatorChar).Last(); + var match = FormatVersionRegex.Match(actualFileName); if (match.Success) { return match.Groups["version"].Value; } - else + + if (inputFileName.Contains(".create.template")) { - throw new ArgumentException($"Format version could not be determined: {part}", nameof(inputFileName)); + throw new ArgumentException("Pass the name of the \".template\" file, not \".create.template\"", nameof(inputFileName)); } + throw new ArgumentException($"Format version could not be determined: {actualFileName}", nameof(inputFileName)); } // the format version will be written to the json template file, thus needs to be passed diff --git a/EDILibraryTests/TemplateHelperTests.cs b/EDILibraryTests/TemplateHelperTests.cs new file mode 100644 index 0000000..6e00df8 --- /dev/null +++ b/EDILibraryTests/TemplateHelperTests.cs @@ -0,0 +1,27 @@ +using System.IO; +using EDILibrary; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace EDILibraryTests; + +[TestClass] +public class TemplateHelperTests +{ + [TestMethod] + [DataRow("COMDIS1.0c.template", "1.0c")] + [DataRow("UTILMDGG1.0a.template", "G1.0a")] + public void Test_RetrieveFormatVersionFromInputFileName(string filename, string expectedFormatVersion) + { + var actualFormatVersion = TemplateHelper.RetrieveFormatVersionFromInputFileName(filename); + Assert.AreEqual(expectedFormatVersion, actualFormatVersion); + } + + [TestMethod] + public void Test_RetrieveFormatVersionFromInputFileName_ForPaths() + { + var filepathAsString = $"Path{Path.DirectorySeparatorChar}To{Path.DirectorySeparatorChar}My{Path.DirectorySeparatorChar}Favourite{Path.DirectorySeparatorChar}Templates{Path.DirectorySeparatorChar}folder{Path.DirectorySeparatorChar}COMDIS1.0c.template"; + var actualFormatVersion = TemplateHelper.RetrieveFormatVersionFromInputFileName(filepathAsString); + Assert.AreEqual("1.0c", actualFormatVersion); + } + +}