< Summary

Information
Class: Morpho25.IO.Library
Assembly: Morpho25
File(s): D:\a\Morpho\Morpho\project\Morpho\Morpho25\IO\Library.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 45
Coverable lines: 45
Total lines: 121
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 10
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
get_Code()100%10%
get_Description()100%10%
get_Detail()100%10%
.ctor(...)100%10%
GetCompatibleText(...)0%20%
SetLibrary(...)0%80%

File(s)

D:\a\Morpho\Morpho\project\Morpho\Morpho25\IO\Library.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5using System.Text.RegularExpressions;
 6using System.Threading.Tasks;
 7using System.Xml;
 8
 9namespace Morpho25.IO
 10{
 11    /// <summary>
 12    /// Library material class.
 13    /// </summary>
 14    public class Library
 15    {
 16        /// <summary>
 17        /// Soil type (for soil).
 18        /// </summary>
 19        public const string SOIL = "SOIL";
 20        /// <summary>
 21        /// Profile type (for soil).
 22        /// </summary>
 23        public const string PROFILE = "PROFILE";
 24        /// <summary>
 25        /// Material type (for buildings).
 26        /// </summary>
 27        public const string MATERIAL = "MATERIAL";
 28        /// <summary>
 29        /// Wall type (for buildings).
 30        /// </summary>
 31        public const string WALL = "WALL";
 32        /// <summary>
 33        /// Source type (for source).
 34        /// </summary>
 35        public const string SOURCE = "SOURCE";
 36        /// <summary>
 37        /// Plant type (for plant 2D).
 38        /// </summary>
 39        public const string PLANT = "PLANT";
 40        /// <summary>
 41        /// Plant 3D type (for plant 3D).
 42        /// </summary>
 43        public const string PLANT3D = "PLANT3D";
 44        /// <summary>
 45        /// Greening type (for building).
 46        /// </summary>
 47        public const string GREENING = "GREENING";
 48        /// <summary>
 49        /// Collection of material's code.
 50        /// </summary>
 051        public List<string> Code { get; private set; }
 52        /// <summary>
 53        /// Collection of material's description.
 54        /// </summary>
 055        public List<string> Description { get; private set; }
 56        /// <summary>
 57        /// Collection of material's detail.
 58        /// </summary>
 059        public List<string> Detail { get; private set; }
 60        /// <summary>
 61        /// Create a new library object.
 62        /// </summary>
 63        /// <param name="file">File path of the DB to read.</param>
 64        /// <param name="type">Section of DB to read.</param>
 65        /// <param name="keyword">Filter by keyword.</param>
 066        public Library(string file,
 067            string type, string keyword)
 068        {
 069            Code = new List<string>();
 070            Description = new List<string>();
 071            Detail = new List<string>();
 72
 073            SetLibrary(file, type, keyword);
 074        }
 75
 76        private string GetCompatibleText(string file)
 077        {
 078            string characters = @"[^\s()_<>/,\.A-Za-z0-9=""]+";
 79
 080            Encoding iso = Encoding.GetEncoding("ISO-8859-1");
 081            if (!System.IO.File.Exists(file))
 082                throw new Exception($"{file} not found.");
 083            string text = System.IO.File.ReadAllText(file, iso);
 084            string res = Regex.Replace(text, characters, "");
 85
 086            return res;
 087        }
 88
 89        private void SetLibrary(string file, string type, string keyword)
 090        {
 091            string innerText = GetCompatibleText(file);
 92
 093            string word = (type != GREENING) ? "Description" : "Name";
 94
 095            XmlDocument xmlDcoument = new XmlDocument();
 096            xmlDcoument.LoadXml(innerText);
 097            XmlNodeList data = xmlDcoument.DocumentElement.SelectNodes(type);
 98
 099            var idContainer = new string[data.Count];
 0100            var descriptionContainer = new string[data.Count];
 0101            var dataContainer = new string[data.Count];
 102
 0103            Parallel.For(0, data.Count, i =>
 0104            {
 0105                var description = data[i].SelectSingleNode(word).InnerText;
 0106                if (keyword != null)
 0107                    if (!description.ToUpper()
 0108                    .Contains(keyword?.ToUpper())) return;
 0109
 0110                dataContainer[i] = data[i].OuterXml;
 0111                descriptionContainer[i]= description;
 0112                var id = data[i].SelectSingleNode("ID").InnerText;
 0113                idContainer[i] = id.Replace(" ", "");
 0114            });
 115
 0116            Code.AddRange(idContainer.Where(_ => _ != null));
 0117            Description.AddRange(descriptionContainer.Where(_ => _ != null));
 0118            Detail.AddRange(dataContainer.Where(_ => _ != null));
 0119        }
 120    }
 121}