< Summary - SonghayCore

Information
Class: Songhay.Xml.OpmlUtility
Assembly: SonghayCore
File(s): /home/rasx/sourceRoot/SonghayCore/SonghayCore/Xml/OpmlUtility.Get.cs
Line coverage
68%
Covered lines: 84
Uncovered lines: 38
Coverable lines: 122
Total lines: 220
Line coverage: 68.8%
Branch coverage
54%
Covered branches: 25
Total branches: 46
Branch coverage: 54.3%
Method coverage

Method coverage is only available for sponsors.

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
get_rx()100%1100%
GetBody(...)50%2100%
GetDocument(...)60%1087.5%
GetDocument(...)100%10%
GetDocument(...)50%2100%
GetHead(...)50%2100%
GetOutline(...)50%2100%
GetOutlines(...)75%4100%
GetBody(...)0%20%
GetHead(...)64.28%14100%
GetRawDocument(...)0%20%
GetOutline(...)75%494.11%
GetOutlines(...)0%20%

File(s)

/home/rasx/sourceRoot/SonghayCore/SonghayCore/Xml/OpmlUtility.Get.cs

#LineLine coverage
 1namespace Songhay.Xml;
 2
 3/// <summary>
 4/// Static helpers for OPML.
 5/// </summary>
 6public static class OpmlUtility
 7{
 8    /// <summary>
 9    /// Conventional namespace for OPML documents.
 10    /// </summary>
 11    // ReSharper disable once InconsistentNaming
 412    public static XNamespace rx => "http://songhaysystem.com/schemas/opml.xsd";
 13
 14    /// <summary>
 15    /// Gets the <see cref="OpmlBody"/>.
 16    /// </summary>
 17    /// <param name="root">The root.</param>
 18    /// <param name="ns">The namespace.</param>
 19    public static OpmlBody? GetBody(XContainer? root, XNamespace? ns)
 520    {
 521        if (root == null) return null;
 22
 523        var data = new OpmlBody
 524        {
 525            Outlines = GetOutlines(root, ns)
 526        };
 27
 528        return data;
 529    }
 30
 31    /// <summary>
 32    /// Gets the <see cref="OpmlDocument"/>.
 33    /// </summary>
 34    /// <param name="path">The path.</param>
 35    public static OpmlDocument? GetDocument(string? path)
 536    {
 537        var xd = XDocument.Load(path!);
 38
 539        XNamespace? ns = xd.Root?.GetDefaultNamespace();
 840        if (ns != null && !string.IsNullOrWhiteSpace(ns.ToString())) return GetDocument(xd.Root, ns);
 41
 242        ns = xd.Root?.GetNamespaceOfPrefix(nameof(rx));
 443        if (ns == rx) return GetDocument(xd.Root, rx);
 44
 045        return GetRawDocument(xd);
 546    }
 47
 48    /// <summary>
 49    /// Gets the <see cref="OpmlDocument"/>.
 50    /// </summary>
 51    /// <param name="xml">The XML.</param>
 52    /// <param name="ns">The ns.</param>
 53    public static OpmlDocument? GetDocument(string? xml, XNamespace? ns)
 054    {
 055        var xd = XDocument.Parse(xml!);
 056        var opml = GetDocument(xd.Root, ns);
 57
 058        return opml;
 059    }
 60
 61    /// <summary>
 62    /// Gets the <see cref="OpmlDocument"/>.
 63    /// </summary>
 64    /// <param name="root">The root.</param>
 65    /// <param name="ns">The conventional namespace.</param>
 66    public static OpmlDocument? GetDocument(XContainer? root, XNamespace? ns)
 567    {
 568        if (root == null) return null;
 69
 570        var data = new OpmlDocument
 571        {
 572            OpmlBody = GetBody(root.Element(ns.ToXName("body")!), ns),
 573            OpmlHead = GetHead(root.Element(ns.ToXName("head")!), ns)
 574        };
 75
 576        return data;
 577    }
 78
 79    /// <summary>
 80    /// Gets the <see cref="OpmlHead"/>.
 81    /// </summary>
 82    /// <param name="root">The root.</param>
 83    /// <param name="ns">The namespace.</param>
 84    public static OpmlHead? GetHead(XContainer? root, XNamespace? ns)
 585    {
 586        if (root == null) return null;
 87
 588        var ownerEmail = ns.ToXName("ownerEmail");
 589        var ownerName = ns.ToXName("ownerName");
 590        var title = ns.ToXName("title");
 591        var dateCreated = ns.ToXName("dateCreated");
 592        var dateModified = ns.ToXName("dateModified");
 93
 594        return GetHead(root, ownerEmail, ownerName, title, dateCreated, dateModified);
 595    }
 96
 97    /// <summary>
 98    /// Gets the <see cref="OpmlOutline"/>.
 99    /// </summary>
 100    /// <param name="root">The root.</param>
 101    /// <param name="ns">The namespace.</param>
 102    public static OpmlOutline? GetOutline(XElement? root, XNamespace? ns)
 8568103    {
 8568104        if (root == null) return null;
 105
 8568106        var data = GetOutline(root);
 8568107        data.Outlines = GetOutlines(root, ns);
 108
 8568109        return data;
 8568110    }
 111
 112    /// <summary>
 113    /// Gets the array of <see cref="OpmlOutline"/>.
 114    /// </summary>
 115    /// <param name="root">The root.</param>
 116    /// <param name="ns">The namespace.</param>
 117    public static OpmlOutline[] GetOutlines(XContainer? root, XNamespace? ns)
 17141118    {
 17141119        if (root == null) return Enumerable.Empty<OpmlOutline>().ToArray();
 120
 17141121        var data = new List<OpmlOutline>();
 17141122        root
 17141123            .Elements(ns.ToXName("outline"))
 17141124            .ForEachInEnumerable(o =>
 8568125            {
 8568126                var outline = GetOutline(o, ns);
 8568127                if (outline == null) return;
 17141128
 8568129                outline.Outlines = GetOutlines(o, ns);
 8568130                data.Add(outline);
 25709131            });
 132
 17141133        return data.ToArray();
 17141134    }
 135
 136    static OpmlBody? GetBody(XContainer? root)
 0137    {
 0138        if (root == null) return null;
 139
 0140        var data = new OpmlBody
 0141        {
 0142            Outlines = GetOutlines(root)
 0143        };
 144
 0145        return data;
 0146    }
 147
 148    static OpmlHead GetHead(XContainer? root, XName? ownerEmail, XName? ownerName, XName? title, XName? dateCreated,
 149        XName? dateModified)
 5150    {
 5151        var data = new OpmlHead
 5152        {
 5153            OwnerEmail = root?.Elements(ownerEmail).ToElementValueOrNull(),
 5154            OwnerName = root?.Elements(ownerName).ToElementValueOrNull(),
 5155            Title = root?.Elements(title).ToElementValueOrNull()
 5156        };
 157
 158        string? s;
 159
 5160        s = root?.Elements(dateCreated).ToElementValueOrNull();
 10161        if (!string.IsNullOrWhiteSpace(s)) data.DateCreated = ProgramTypeUtility.ParseRfc822DateTime(s);
 162
 5163        s = root?.Elements(dateModified).ToElementValueOrNull();
 10164        if (!string.IsNullOrWhiteSpace(s)) data.DateModified = ProgramTypeUtility.ParseRfc822DateTime(s);
 165
 5166        return data;
 5167    }
 168
 169    static OpmlDocument? GetRawDocument(XContainer? root)
 0170    {
 0171        if (root == null) return null;
 172
 0173        var data = new OpmlDocument
 0174        {
 0175            OpmlBody = GetBody(root.Descendants("body").FirstOrDefault()),
 0176            OpmlHead = GetHead(root.Descendants("head").FirstOrDefault(), "ownerEmail", "ownerName", "title",
 0177                "dateCreated", "dateModified")
 0178        };
 179
 0180        return data;
 0181    }
 182
 183    static OpmlOutline GetOutline(XElement? root)
 8568184    {
 8568185        var data = new OpmlOutline
 8568186        {
 8568187            Category = root.ToAttributeValueOrNull("category"),
 8568188            Id = root.ToAttributeValueOrNull("id"),
 8568189            Text = root.ToAttributeValueOrNull("text"),
 8568190            Title = root.ToAttributeValueOrNull("title"),
 8568191            OutlineType = root.ToAttributeValueOrNull("type"),
 8568192            Url = root.ToAttributeValueOrNull("url"),
 8568193            XmlUrl = root.ToAttributeValueOrNull("xmlUrl")
 8568194        };
 195
 8568196        if (data.Url != null)
 8272197            data.Url = Environment.ExpandEnvironmentVariables(data.Url);
 8568198        if (data.XmlUrl != null)
 0199            data.XmlUrl = Environment.ExpandEnvironmentVariables(data.XmlUrl);
 200
 8568201        return data;
 8568202    }
 203
 204    static OpmlOutline[] GetOutlines(XContainer? root)
 0205    {
 0206        if (root == null) return Enumerable.Empty<OpmlOutline>().ToArray();
 207
 0208        var data = new List<OpmlOutline>();
 0209        root
 0210            .Elements("outline")
 0211            .ForEachInEnumerable(o =>
 0212            {
 0213                var outline = GetOutline(o);
 0214                outline.Outlines = GetOutlines(o);
 0215                data.Add(outline);
 0216            });
 217
 0218        return data.ToArray();
 0219    }
 220}