Skip to content

Commit

Permalink
ApplyTmTemplate
Browse files Browse the repository at this point in the history
 -> adds Validation method in TemplateLoader
 -> refactoring of ExportImportClass
  • Loading branch information
ealbu committed Apr 5, 2019
1 parent c0ceb3d commit 0b4f9c0
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,39 +38,43 @@ public void ImportResourcesFromExcel(string filePathFrom, string filePathTo, Set
using (var package = GetExcelPackage(filePathFrom))
{
var newLanguageResourceBundles = new List<LanguageResourceBundle>();
NewMethod(settings, deserializedTemplate, package, newLanguageResourceBundles);

foreach (var workSheet in package.Workbook.Worksheets)
{
var column01 = workSheet.Cells[1, 1].Value;
var column02 = workSheet.Cells[1, 2].Value;
var column03 = workSheet.Cells[1, 3].Value;
var column04 = workSheet.Cells[1, 4].Value;
deserializedTemplate.SaveAs(filePathTo);
}
}

if (AreColumnsValid(column01, column02, column03, column04)) return;
private void NewMethod(Settings settings, FileBasedLanguageResourcesTemplate deserializedTemplate, ExcelPackage package, List<LanguageResourceBundle> newLanguageResourceBundles)
{
foreach (var workSheet in package.Workbook.Worksheets)
{
var column01 = workSheet.Cells[1, 1].Value;
var column02 = workSheet.Cells[1, 2].Value;
var column03 = workSheet.Cells[1, 3].Value;
var column04 = workSheet.Cells[1, 4].Value;

var abbreviations = new Wordlist();
var ordinalFollowers = new Wordlist();
var variables = new Wordlist();
var segmentationRules = new SegmentationRules();
if (AreColumnsValid(column01, column02, column03, column04)) return;

ReadFromExcel(workSheet, abbreviations, ordinalFollowers, variables, segmentationRules);
var abbreviations = new Wordlist();
var ordinalFollowers = new Wordlist();
var variables = new Wordlist();
var segmentationRules = new SegmentationRules();

var newLanguageResourceBundle =
new LanguageResourceBundle(CultureInfoExtensions.GetCultureInfo(workSheet.Name))
{
Abbreviations = abbreviations,
OrdinalFollowers = ordinalFollowers,
Variables = variables,
SegmentationRules = segmentationRules
};
ReadFromExcel(workSheet, abbreviations, ordinalFollowers, variables, segmentationRules);

newLanguageResourceBundles.Add(newLanguageResourceBundle);
}
ExcludeWhatIsNotNeeded(newLanguageResourceBundles, settings);
AddNewBundles(deserializedTemplate, newLanguageResourceBundles);
var newLanguageResourceBundle =
new LanguageResourceBundle(CultureInfoExtensions.GetCultureInfo(workSheet.Name))
{
Abbreviations = abbreviations,
OrdinalFollowers = ordinalFollowers,
Variables = variables,
SegmentationRules = segmentationRules
};

deserializedTemplate.SaveAs(filePathTo);
newLanguageResourceBundles.Add(newLanguageResourceBundle);
}
ExcludeWhatIsNotNeeded(newLanguageResourceBundles, settings);
AddNewBundles(deserializedTemplate, newLanguageResourceBundles);
}

public void ExcludeWhatIsNotNeeded(List<LanguageResourceBundle> languageResourceBundles, Settings settings)
Expand Down Expand Up @@ -186,65 +190,10 @@ private void AddNewBundles(FileBasedLanguageResourcesTemplate deserializedTempla
//in case there is already a bundle for that culture, we need to go into more detail and see what to add and what is already there
if (correspondingBundleInTemplate != null)
{
if (newBundle.Abbreviations != null)
{
if (correspondingBundleInTemplate.Abbreviations != null)
{
AddItemsToWordlist(newBundle, correspondingBundleInTemplate, "Abbreviations");
}
else
{
correspondingBundleInTemplate.Abbreviations = new Wordlist(newBundle.Abbreviations);
}
}

if (newBundle.OrdinalFollowers != null)
{
if (correspondingBundleInTemplate.OrdinalFollowers != null)
{
AddItemsToWordlist(newBundle, correspondingBundleInTemplate, "OrdinalFollowers");
}
else
{
correspondingBundleInTemplate.OrdinalFollowers = new Wordlist(newBundle.OrdinalFollowers);
}
}

if (newBundle.Variables != null)
{
if (correspondingBundleInTemplate.Variables != null)
{
AddItemsToWordlist(newBundle, correspondingBundleInTemplate, "Variables");
}
else
{
correspondingBundleInTemplate.Variables = new Wordlist(newBundle.Variables);
}
}

if (newBundle.SegmentationRules != null)
{
if (correspondingBundleInTemplate.SegmentationRules != null)
{
var newSegmentationRules = new SegmentationRules();
foreach (var newRule in newBundle.SegmentationRules.Rules)
{
if (correspondingBundleInTemplate.SegmentationRules.Rules.All(oldRule =>
string.Equals(newRule.Description.Text, oldRule.Description.Text,
StringComparison.OrdinalIgnoreCase)))
{
newSegmentationRules.AddRule(newRule);
}
}

correspondingBundleInTemplate.SegmentationRules.Rules.AddRange(newSegmentationRules.Rules);
}
else
{
correspondingBundleInTemplate.SegmentationRules =
new SegmentationRules(newBundle.SegmentationRules);
}
}
AddItemsToWordlist(newBundle, correspondingBundleInTemplate, "Abbreviations");
AddItemsToWordlist(newBundle, correspondingBundleInTemplate, "OrdinalFollowers");
AddItemsToWordlist(newBundle, correspondingBundleInTemplate, "Variables");
AddSegmentationRulesToBundle(newBundle, correspondingBundleInTemplate);
}
//otherwise, just add the newBundle
else
Expand All @@ -254,18 +203,50 @@ private void AddNewBundles(FileBasedLanguageResourcesTemplate deserializedTempla
}
}

private static void AddItemsToWordlist(LanguageResourceBundle newLanguageResourceBundle, LanguageResourceBundle defaultOrOldTemplate, string property)
private static void AddSegmentationRulesToBundle(LanguageResourceBundle newBundle, LanguageResourceBundle correspondingBundleInTemplate)
{
var getterOfDefaultOrOldTemplate = (typeof(LanguageResourceBundle).GetProperty(property)?.GetMethod.Invoke(defaultOrOldTemplate, null) as Wordlist);
var getterOfNewLanguageResourceBundle = (typeof(LanguageResourceBundle).GetProperty(property)?.GetMethod.Invoke(newLanguageResourceBundle, null) as Wordlist);
if (newBundle.SegmentationRules == null) return;
if (correspondingBundleInTemplate.SegmentationRules != null)
{
var newSegmentationRules = new SegmentationRules();
foreach (var newRule in newBundle.SegmentationRules.Rules)
{
if (correspondingBundleInTemplate.SegmentationRules.Rules.All(oldRule =>
string.Equals(newRule.Description.Text, oldRule.Description.Text,
StringComparison.OrdinalIgnoreCase)))
{
newSegmentationRules.AddRule(newRule);
}
}

if (getterOfNewLanguageResourceBundle != null && getterOfNewLanguageResourceBundle.Items.Any())
correspondingBundleInTemplate.SegmentationRules.Rules.AddRange(newSegmentationRules.Rules);
}
else
{
foreach (var abbrev in getterOfNewLanguageResourceBundle.Items)
correspondingBundleInTemplate.SegmentationRules =
new SegmentationRules(newBundle.SegmentationRules);
}
}

private static void AddItemsToWordlist(LanguageResourceBundle newLanguageResourceBundle, LanguageResourceBundle template, string property)
{
var templateBundleGetter = (typeof(LanguageResourceBundle).GetProperty(property)?.GetMethod.Invoke(template, null) as Wordlist);
var templateBundleSetter = typeof(LanguageResourceBundle).GetProperty(property)?.SetMethod;
var newBundleGetter = (typeof(LanguageResourceBundle).GetProperty(property)?.GetMethod.Invoke(newLanguageResourceBundle, null) as Wordlist);

if (newBundleGetter == null || !newBundleGetter.Items.Any()) return;

if (templateBundleGetter != null && templateBundleGetter.Items.Any())
{
foreach (var abbrev in newBundleGetter.Items)
{
getterOfDefaultOrOldTemplate?.Add(abbrev);
templateBundleGetter.Add(abbrev);
}
}
else
{
templateBundleSetter?.Invoke(template, new [] {new Wordlist(newBundleGetter)});
}
}

private ExcelPackage GetExcelPackage(string filePath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,33 +59,7 @@ public string GetTmFolderPath()

public List<LanguageResourceBundle> GetLanguageResourceBundlesFromFile(string resourceTemplatePath, out string message, out List<int> unIDedLanguages)
{
message = "";
unIDedLanguages = new List<int>();

if (string.IsNullOrEmpty(resourceTemplatePath))
{
message = "Select a template";
return null;
}

if (!File.Exists(resourceTemplatePath))
{
message = "The file path of the template is not correct!";
return null;
}

if (Path.GetExtension(resourceTemplatePath) != ".resource")
{
message = @"The file is not of the required type, ""resource""";
return null;
}

var lrt = LoadDataFromFile(resourceTemplatePath, "LanguageResource");

if (lrt.Count == 0)
{
message = "This template is corrupted or the file is not a template";
}
if (ValidateFile(resourceTemplatePath, out message, out unIDedLanguages, out var lrt)) return null;

var langResBundlesList = new List<LanguageResourceBundle>();

Expand Down Expand Up @@ -124,6 +98,40 @@ public List<LanguageResourceBundle> GetLanguageResourceBundlesFromFile(string re
return langResBundlesList;
}

private bool ValidateFile(string resourceTemplatePath, out string message, out List<int> unIDedLanguages, out XmlNodeList lrt)
{
message = "";
unIDedLanguages = new List<int>();
lrt = null;

if (string.IsNullOrEmpty(resourceTemplatePath))
{
message = "Select a template";
return true;
}

if (!File.Exists(resourceTemplatePath))
{
message = "The file path of the template is not correct!";
return true;
}

if (Path.GetExtension(resourceTemplatePath) != ".resource")
{
message = @"The file is not of the required type, ""resource""";
return true;
}

lrt = LoadDataFromFile(resourceTemplatePath, "LanguageResource");

if (lrt.Count == 0)
{
message = "This template is corrupted or the file is not a template";
}

return false;
}

public XmlNodeList LoadDataFromFile(string filePath, string element)
{
var doc = new XmlDocument();
Expand Down

0 comments on commit 0b4f9c0

Please sign in to comment.