Refactor code for XML document creation

The XML document creation has been refactored into a separate function to reduce repetition. Prior code for creating the XML document was repeated in several places, specifically in the 'Remove' and 'Add' functions. The new 'CreateDocument' function now contains this code, making the overall codebase cleaner and easier to maintain.

Signed-off-by: Ivandro Jao <ivandrofly@gmail.com>
This commit is contained in:
Ivandro Jao 2024-05-08 13:20:06 +01:00
parent e91bf3d1b7
commit 2b726092e8

View File

@ -43,6 +43,7 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
{ {
LoadNamesList(Path.Combine(_dictionaryFolder, "names.xml")); LoadNamesList(Path.Combine(_dictionaryFolder, "names.xml"));
} }
foreach (var name in _blackList) foreach (var name in _blackList)
{ {
if (_namesList.Contains(name)) if (_namesList.Contains(name))
@ -97,6 +98,7 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
{ {
twoLetterIsoLanguageName = LanguageName.Substring(0, 2); twoLetterIsoLanguageName = LanguageName.Substring(0, 2);
} }
return Path.Combine(_dictionaryFolder, twoLetterIsoLanguageName + "_names.xml"); return Path.Combine(_dictionaryFolder, twoLetterIsoLanguageName + "_names.xml");
} }
@ -171,15 +173,7 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
_namesMultiList.Remove(name); _namesMultiList.Remove(name);
var fileName = GetLocalNamesUserFileName(); var fileName = GetLocalNamesUserFileName();
var nameListXml = new XmlDocument { XmlResolver = null }; var nameListXml = CreateDocument(fileName);
if (File.Exists(fileName))
{
nameListXml.Load(fileName);
}
else
{
nameListXml.LoadXml("<names><blacklist></blacklist></names>");
}
// Add removed name to blacklist // Add removed name to blacklist
var nameNode = nameListXml.CreateElement("name"); var nameNode = nameListXml.CreateElement("name");
@ -208,6 +202,7 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
nameListXml.DocumentElement.RemoveChild(nodeToRemove); nameListXml.DocumentElement.RemoveChild(nodeToRemove);
} }
} }
try try
{ {
nameListXml.Save(fileName); nameListXml.Save(fileName);
@ -218,6 +213,7 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
System.Diagnostics.Debug.WriteLine("NamesList.Remove failed"); System.Diagnostics.Debug.WriteLine("NamesList.Remove failed");
} }
} }
return false; return false;
} }
@ -254,15 +250,7 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
// <two-letter-iso-code>_names.xml, e.g "en_names.xml" // <two-letter-iso-code>_names.xml, e.g "en_names.xml"
var fileName = GetLocalNamesUserFileName(); var fileName = GetLocalNamesUserFileName();
var nameListXml = new XmlDocument { XmlResolver = null }; var nameListXml = CreateDocument(fileName);
if (File.Exists(fileName))
{
nameListXml.Load(fileName);
}
else
{
nameListXml.LoadXml("<names><blacklist></blacklist></names>");
}
var de = nameListXml.DocumentElement; var de = nameListXml.DocumentElement;
if (de != null) if (de != null)
@ -272,9 +260,25 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
de.AppendChild(node); de.AppendChild(node);
nameListXml.Save(fileName); nameListXml.Save(fileName);
} }
return true; return true;
} }
private static XmlDocument CreateDocument(string fileName)
{
var xmlDocument = new XmlDocument { XmlResolver = null };
if (File.Exists(fileName))
{
xmlDocument.Load(fileName);
}
else
{
xmlDocument.LoadXml("<names><blacklist></blacklist></names>");
}
return xmlDocument;
}
public bool IsInNamesMultiWordList(string input, string word) public bool IsInNamesMultiWordList(string input, string word)
{ {
if (string.IsNullOrEmpty(input) || string.IsNullOrEmpty(word)) if (string.IsNullOrEmpty(input) || string.IsNullOrEmpty(word))
@ -327,6 +331,7 @@ namespace Nikse.SubtitleEdit.Core.Dictionaries
return true; return true;
} }
} }
return false; return false;
} }