Skip to content

Commit

Permalink
Document Assembler - Small Code Reorganisation (#83)
Browse files Browse the repository at this point in the history
* Moved a number of classes and methods outside of DocumentAssembler.cs ahead of adding improvements to DocumentAssembler.
Tests passing.

* fix: format code

---------

Co-authored-by: Sergey Tihon <[email protected]>
  • Loading branch information
MalcolmJohnston and sergey-tihon authored Nov 1, 2024
1 parent 87a65c1 commit 0962735
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 181 deletions.
60 changes: 60 additions & 0 deletions Clippit/Word/Assembler/ErrorHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Clippit.Word.Assembler
{
internal static class ErrorHandler
{
internal static object CreateContextErrorMessage(
this XElement element,
string errorMessage,
TemplateError templateError
)
{
XElement para = element.Descendants(W.p).FirstOrDefault();
XElement run = element.Descendants(W.r).FirstOrDefault();
var errorRun = CreateRunErrorMessage(errorMessage, templateError);
if (para != null)
return new XElement(W.p, errorRun);
else
return errorRun;
}

internal static XElement CreateRunErrorMessage(string errorMessage, TemplateError templateError)
{
templateError.HasError = true;
var errorRun = new XElement(
W.r,
new XElement(
W.rPr,
new XElement(W.color, new XAttribute(W.val, "FF0000")),
new XElement(W.highlight, new XAttribute(W.val, "yellow"))
),
new XElement(W.t, errorMessage)
);
return errorRun;
}

internal static XElement CreateParaErrorMessage(string errorMessage, TemplateError templateError)
{
templateError.HasError = true;
var errorPara = new XElement(
W.p,
new XElement(
W.r,
new XElement(
W.rPr,
new XElement(W.color, new XAttribute(W.val, "FF0000")),
new XElement(W.highlight, new XAttribute(W.val, "yellow"))
),
new XElement(W.t, errorMessage)
)
);
return errorPara;
}
}
}
27 changes: 27 additions & 0 deletions Clippit/Word/Assembler/PA.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Xml.Linq;

namespace Clippit.Word.Assembler
{
internal static class PA
{
public static XName Image = "Image";
public static XName Content = "Content";
public static XName DocumentTemplate = "DocumentTemplate";
public static XName Document = "Document";
public static XName Table = "Table";
public static XName Repeat = "Repeat";
public static XName EndRepeat = "EndRepeat";
public static XName Conditional = "Conditional";
public static XName EndConditional = "EndConditional";

public static XName Select = "Select";
public static XName Optional = "Optional";
public static XName Match = "Match";
public static XName NotMatch = "NotMatch";
public static XName Depth = "Depth";
public static XName Align = "Align";
public static XName Path = "Path";
public static XName Data = "Data";
public static XName PageBreakAfter = "PageBreakAfter";
}
}
10 changes: 10 additions & 0 deletions Clippit/Word/Assembler/PASchemaSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Xml.Schema;

namespace Clippit.Word.Assembler
{
internal class PASchemaSet
{
public string XsdMarkup;
public XmlSchemaSet SchemaSet;
}
}
7 changes: 7 additions & 0 deletions Clippit/Word/Assembler/TemplateError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Clippit.Word.Assembler
{
internal class TemplateError
{
internal bool HasError { get; set; }
}
}
69 changes: 69 additions & 0 deletions Clippit/Word/Assembler/XPathExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;

namespace Clippit.Word.Assembler
{
internal static class XPathExtensions
{
internal static string[] EvaluateXPath(this XElement element, string xPath, bool optional)
{
object xPathSelectResult;
try
{
// support some cells in the table may not have an xpath expression.
if (string.IsNullOrWhiteSpace(xPath))
{
return [];
}

xPathSelectResult = element.XPathEvaluate(xPath);
}
catch (XPathException e)
{
throw new XPathException("XPathException: " + e.Message, e);
}

if (xPathSelectResult is IEnumerable enumerable and not string)
{
var result = enumerable
.Cast<XObject>()
.Select(x =>
x switch
{
XElement xElement => xElement.Value,
XAttribute attribute => attribute.Value,
_ => throw new ArgumentException($"Unknown element type: {x.GetType().Name}"),
}
)
.ToArray();

if (result.Length == 0 && !optional)
throw new XPathException($"XPath expression ({xPath}) returned no results");
return result;
}

return new[] { xPathSelectResult.ToString() };
}

internal static string EvaluateXPathToString(this XElement element, string xPath, bool optional)
{
var selectedData = element.EvaluateXPath(xPath, true);

return selectedData.Length switch
{
0 when optional => string.Empty,
0 => throw new XPathException($"XPath expression ({xPath}) returned no results"),
> 1 => throw new XPathException($"XPath expression ({xPath}) returned more than one node"),
_ => selectedData.First(),
};
}
}
}
Loading

0 comments on commit 0962735

Please sign in to comment.