Skip to content

Commit

Permalink
Save translation with self-closing XML element tags
Browse files Browse the repository at this point in the history
  • Loading branch information
xylographe committed Aug 15, 2019
1 parent ff3c114 commit 15898b7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
15 changes: 8 additions & 7 deletions src/XmlContentTranslator/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private bool OpenFirstXmlDocument(XmlDocument doc)
var treeNode = new TreeNode(childNode.Name);
treeNode.Tag = childNode;
treeView1.Nodes.Add(treeNode);
if (childNode.ChildNodes.Count > 0 && !XmlUtils.IsTextNode(childNode))
if (childNode.ChildNodes.Count > 0 && !childNode.IsTextNode())
{
ExpandNode(treeNode, childNode);
}
Expand Down Expand Up @@ -201,7 +201,7 @@ private void OpenSecondFile(string fileName)
{
foreach (XmlNode childNode in doc.DocumentElement.ChildNodes)
{
if (childNode.ChildNodes.Count > 0 && !XmlUtils.IsTextNode(childNode))
if (childNode.ChildNodes.Count > 0 && !childNode.IsTextNode())
{
ExpandNode(null, childNode);
}
Expand Down Expand Up @@ -309,7 +309,7 @@ private void ExpandNode(TreeNode parentNode, XmlNode node)
treeView1.Nodes.Add(treeNode);
else
parentNode.Nodes.Add(treeNode);
if (XmlUtils.IsParentElement(childNode))
if (childNode.IsParentElement())
{
ExpandNode(treeNode, childNode);
}
Expand All @@ -326,7 +326,7 @@ private void ExpandNode(TreeNode parentNode, XmlNode node)
AddAttributes(node);
foreach (XmlNode childNode in node.ChildNodes)
{
if (XmlUtils.IsParentElement(childNode))
if (childNode.IsParentElement())
{
ExpandNode(null, childNode);
}
Expand Down Expand Up @@ -425,7 +425,7 @@ private void FillOriginalDocumentFromSecondLanguage()
{
foreach (XmlNode childNode in _originalDocument.DocumentElement.ChildNodes)
{
if (childNode.ChildNodes.Count > 0 && !XmlUtils.IsTextNode(childNode))
if (childNode.ChildNodes.Count > 0 && !childNode.IsTextNode())
{
FillOriginalDocumentExpandNode(childNode);
}
Expand All @@ -439,6 +439,7 @@ private void FillOriginalDocumentFromSecondLanguage()
FillAttributes(_originalDocument.DocumentElement);
}
}
XmlUtils.ConvertToSelfClosingTags(_originalDocument.DocumentElement);
}
}

Expand All @@ -447,7 +448,7 @@ private void FillOriginalDocumentExpandNode(XmlNode node)
FillAttributes(node);
foreach (XmlNode childNode in node.ChildNodes)
{
if (childNode.ChildNodes.Count > 0 && !XmlUtils.IsTextNode(childNode))
if (childNode.ChildNodes.Count > 0 && !childNode.IsTextNode())
{
FillOriginalDocumentExpandNode(childNode);
}
Expand All @@ -465,7 +466,7 @@ private void FillOriginalDocumentExpandNode(XmlNode node)

private void FillAttributes(XmlNode node)
{
if (node.Attributes == null)
if (node == null || node.Attributes == null)
return;

foreach (XmlNode attribute in node.Attributes)
Expand Down
35 changes: 28 additions & 7 deletions src/XmlContentTranslator/XmlUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ public static class XmlUtils
{
private static readonly StringBuilder Sb = new StringBuilder();

public static bool IsTextNode(XmlNode childNode)
public static bool IsTextNode(this XmlNode node)
{
if (childNode.ChildNodes.Count == 1 && childNode.ChildNodes[0].NodeType == XmlNodeType.Text)
return true;
return false;
return node != null && node.ChildNodes.Count == 1 && node.ChildNodes[0].NodeType == XmlNodeType.Text;
}

public static bool IsParentElement(this XmlNode node)
{
return node != null && node.ChildNodes.Count > 0 && !node.IsTextNode() && node.NodeType != XmlNodeType.Comment && node.NodeType != XmlNodeType.CDATA;
}

public static string BuildNodePath(XmlNode node)
Expand Down Expand Up @@ -94,10 +97,28 @@ public static string GetNodeIndex(XmlNode node)
return string.Format("[{0}]", i);
}

public static bool IsParentElement(XmlNode xnode)
public static void ConvertToSelfClosingTags(XmlElement element)
{
return xnode.ChildNodes.Count > 0 && !IsTextNode(xnode) &&
xnode.NodeType != XmlNodeType.Comment && xnode.NodeType != XmlNodeType.CDATA;
if (element != null && !element.IsEmpty)
{
bool noChildElements = true;
if (element.HasChildNodes)
{
for (XmlNode node = element.FirstChild; node != null; node = node.NextSibling)
{
if (node.NodeType == XmlNodeType.Element)
{
ConvertToSelfClosingTags(node as XmlElement);
noChildElements = false;
}
}
}
if (noChildElements && element.InnerText.Length == 0 && element.ParentNode?.ParentNode?.ParentNode != null)
{
element.IsEmpty = true;
}
}
}

}
}

0 comments on commit 15898b7

Please sign in to comment.