< Summary

Information
Class: MorphoReader.Read
Assembly: MorphoReader
File(s): D:\a\Morpho\Morpho\project\Morpho\MorphoReader\Read.cs
Line coverage
96%
Covered lines: 60
Uncovered lines: 2
Coverable lines: 62
Total lines: 107
Line coverage: 96.7%
Branch coverage
50%
Covered branches: 2
Total branches: 4
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
get_Information()100%1100%
.ctor(...)100%1100%
ReadEdxFile(...)50%288.88%
GetValueFromXml(...)50%283.33%
GetDictionaryFromXml(...)100%1100%

File(s)

D:\a\Morpho\Morpho\project\Morpho\MorphoReader\Read.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4using System.Text.RegularExpressions;
 5using System.Xml;
 6
 7namespace MorphoReader
 8{
 9    /// <summary>
 10    /// Read class.
 11    /// </summary>
 12    class Read
 13    {
 14        /// <summary>
 15        /// Binary information.
 16        /// </summary>
 217        public Dictionary<string, string> Information { get; private set; }
 18
 19        /// <summary>
 20        /// Create a new read object.
 21        /// </summary>
 22        /// <param name="path">Full path of the EDT file.</param>
 123        public Read(string path)
 124        {
 125            Information = GetDictionaryFromXml(path);
 126        }
 27
 28        private string ReadEdxFile(string path)
 129        {
 130            string characters = @"[^\s()_<>/,\.A-Za-z0-9=""\P{IsBasicLatin}\p{IsLatin-1Supplement}]+";
 31
 132            var isoLatin1 = Encoding.GetEncoding(28591);
 133            if (!System.IO.File.Exists(path))
 034                throw new Exception($"{path} not found.");
 135            string text = System.IO.File.ReadAllText(path, isoLatin1);
 136            string res = Regex.Replace(text, characters, "");
 37
 138            return res.Replace("<Remark for this Source Type>", "");
 139        }
 40
 41        private static string GetValueFromXml(XmlNodeList nodeList,
 42            string keyword)
 1243        {
 44            // It must be just 1
 4845            foreach (XmlNode child in nodeList)
 1246            {
 1247                return child.SelectSingleNode(keyword).InnerText;
 48            }
 049            return null;
 1250        }
 51
 52        private Dictionary<string, string> GetDictionaryFromXml(string path)
 153        {
 154            Dictionary<string, string> values = new Dictionary<string, string>();
 155            XmlDocument xml = new XmlDocument();
 56
 157            var text = ReadEdxFile(path);
 158            xml.LoadXml(text);
 59
 160            XmlNodeList modeldescription = xml.DocumentElement.SelectNodes("modeldescription");
 161            string projectName = GetValueFromXml(modeldescription, "projectname");
 162            string simulationDate = GetValueFromXml(modeldescription, "simulation_date")
 263                .Replace(" ", ""); ;
 164            string simulationTime = GetValueFromXml(modeldescription, "simulation_time")
 265                .Replace(" ", ""); ;
 166            string locationName = GetValueFromXml(modeldescription, "locationname");
 67
 168            XmlNodeList variables = xml.DocumentElement.SelectNodes("variables");
 169            string nameVariables = GetValueFromXml(variables, "name_variables");
 70
 71
 172            XmlNodeList datadescription = xml.DocumentElement.SelectNodes("datadescription");
 173            string dateContent = GetValueFromXml(datadescription, "data_content");
 174            string spacingX = GetValueFromXml(datadescription, "spacing_x")
 175                .Replace(" ", "");
 176            string spacingY = GetValueFromXml(datadescription, "spacing_y")
 177                .Replace(" ", "");
 178            string spacingZ = GetValueFromXml(datadescription, "spacing_z")
 179                .Replace(" ", "");
 180            string nrXdata = GetValueFromXml(datadescription, "nr_xdata")
 181                .Replace(" ", "");
 182            string nrYdata = GetValueFromXml(datadescription, "nr_ydata")
 183                .Replace(" ", "");
 184            string nrZdata = GetValueFromXml(datadescription, "nr_zdata")
 185                .Replace(" ", "");
 86
 187            values.Add("projectname", projectName);
 188            values.Add("locationname", locationName);
 189            values.Add("simulation_date", simulationDate);
 190            values.Add("simulation_time", simulationTime);
 91
 192            values.Add("data_content", dateContent);
 93
 194            values.Add("spacing_x", spacingX);
 195            values.Add("spacing_y", spacingY);
 196            values.Add("spacing_z", spacingZ);
 97
 198            values.Add("nr_xdata", nrXdata);
 199            values.Add("nr_ydata", nrYdata);
 1100            values.Add("nr_zdata", nrZdata);
 101
 1102            values.Add("name_variables", nameVariables);
 103
 1104            return values;
 1105        }
 106    }
 107}