Skip to content

Commit

Permalink
enable template helper to read formatversion G1.0a etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
hf-fvesely committed Jul 31, 2023
1 parent ff6b236 commit 79757f0
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions EDILibrary/TemplateHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
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;

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 = "^(?<format>[A-Z]{6,7})(?<version>[A-Z]?\\d+\\.\\d+[a-z]?)$";

private static void ParseAPERAKString(string aperak, out string dataType, out int length,
out List<string> list)
{
dataType = null;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit 79757f0

Please sign in to comment.