-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document Assembler - Small Code Reorganisation (#83)
* 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
1 parent
87a65c1
commit 0962735
Showing
6 changed files
with
229 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}; | ||
} | ||
} | ||
} |
Oops, something went wrong.