< Summary - SonghayCore

Information
Class: Songhay.Xml.XhtmlDocumentUtility
Assembly: SonghayCore
File(s): /home/rasx/sourceRoot/SonghayCore/SonghayCore/Xml/XhtmlDocumentUtility.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 57
Coverable lines: 57
Total lines: 112
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 40
Branch coverage: 0%
Method coverage

Method coverage is only available for sponsors.

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
get_Xhtml()100%10%
GetDocument(...)100%10%
GetDocument(...)0%280%
LoadDocument(...)0%100%
WriteDocumentIndex(...)0%20%

File(s)

/home/rasx/sourceRoot/SonghayCore/SonghayCore/Xml/XhtmlDocumentUtility.cs

#LineLine coverage
 1namespace Songhay.Xml;
 2
 3/// <summary>
 4/// Static members for XHTML Documents.
 5/// </summary>
 6public static partial class XhtmlDocumentUtility
 7{
 8    /// <summary>
 9    /// XHTML Namespace
 10    /// </summary>
 011    public static XNamespace Xhtml => "http://www.w3.org/1999/xhtml";
 12
 13    /// <summary>
 14    /// Loads the <see cref="XhtmlDocument"/>.
 15    /// </summary>
 16    /// <param name="document">The XML document.</param>
 17    /// <param name="webPath">The public web path.</param>
 18    public static XhtmlDocument? GetDocument(XDocument? document, string? webPath) =>
 019        GetDocument(document, webPath, true);
 20
 21    /// <summary>
 22    /// Loads the <see cref="XhtmlDocument"/>.
 23    /// </summary>
 24    /// <param name="document">The XML document.</param>
 25    /// <param name="webPath">The public web path.</param>
 26    /// <param name="useXhtmlNamespace">if set to <c>true</c> use XHTML namespace (<c>true</c> by default).</param>
 27    public static XhtmlDocument? GetDocument(XDocument? document, string? webPath, bool useXhtmlNamespace)
 028    {
 029        if (document == null) return null;
 30
 031        var heading = useXhtmlNamespace
 032            ? document.Root?
 033                .Element(Xhtml + "body")?
 034                .Element(Xhtml + "h1")
 035            : document.Root?
 036                .Element("body")?
 037                .Element("h1");
 38
 039        var title = useXhtmlNamespace
 040            ? document.Root?
 041                .Element(Xhtml + "head")?
 042                .Element(Xhtml + "title")?
 043                .Value
 044            : document.Root?
 045                .Element("head")?
 046                .Element("title")?
 047                .Value;
 48
 049        var d = new XhtmlDocument
 050        {
 051            Header = heading?.Value,
 052            Location = webPath,
 053            Title = title
 054        };
 55
 056        return d;
 057    }
 58
 59    /// <summary>
 60    /// Loads the <see cref="XhtmlDocument"/>.
 61    /// </summary>
 62    /// <param name="pathToDocument">The path to document.</param>
 63    /// <param name="webPath">The public web path.</param>
 64    public static XhtmlDocument? LoadDocument(string? pathToDocument, string? webPath)
 065    {
 066        var xd = XDocument.Load(pathToDocument!);
 067        var hasAttributes = (xd.Root?.HasAttributes).GetValueOrDefault();
 068        var hasXhtmlNamespace = false;
 069        if (hasAttributes) hasXhtmlNamespace = (xd.Root?.Attributes("xmlns") ?? Array.Empty<XAttribute>()).Any();
 070        if (hasAttributes && hasXhtmlNamespace)
 071        {
 072            return GetDocument(xd, webPath);
 73        }
 74
 075        return GetDocument(xd, webPath, false);
 076    }
 77
 78    /// <summary>
 79    /// Writes the index of XHTML documents.
 80    /// </summary>
 81    /// <param name="indexFileName">Name of the index file.</param>
 82    /// <param name="indexTitle">The index title.</param>
 83    /// <param name="publicRoot">The public root.</param>
 84    /// <param name="pathToDirectory">The path to the specified directory.</param>
 85    /// <param name="pathToOutput">The path to output.</param>
 86    public static void WriteDocumentIndex(string? indexFileName,
 87        string? indexTitle, string? publicRoot,
 88        string? pathToDirectory, string? pathToOutput)
 089    {
 090        var directory = new DirectoryInfo(pathToDirectory!);
 091        var list = new List<XhtmlDocument>();
 092        directory.GetFiles()
 093            .ForEachInEnumerable(f =>
 094            {
 095                var uri = string.Concat(publicRoot, f.Name);
 096                var d = LoadDocument(f.FullName, uri);
 097                if (d != null) list.Add(d);
 098            });
 99
 0100        var serializer = new XmlSerializer(typeof(XhtmlDocuments));
 101
 0102        using var writer = new XmlTextWriter(string.Concat(pathToOutput, indexFileName), Encoding.UTF8);
 103
 0104        var documents = new XhtmlDocuments
 0105        {
 0106            Documents = list.OrderBy(d => d.Title).ToArray(),
 0107            Title = indexTitle
 0108        };
 109
 0110        serializer.Serialize(writer, documents);
 0111    }
 112}