Index: Core/Components/src/Core.Components.Gis.IO/Readers/WmtsConnectionInfoReader.cs =================================================================== diff -u -r2db5ccc1fb164fc6f43733359a0e745ec6891edd -rf427e6dbf1ae2c7855a72a2f2ce9f458ed6167ba --- Core/Components/src/Core.Components.Gis.IO/Readers/WmtsConnectionInfoReader.cs (.../WmtsConnectionInfoReader.cs) (revision 2db5ccc1fb164fc6f43733359a0e745ec6891edd) +++ Core/Components/src/Core.Components.Gis.IO/Readers/WmtsConnectionInfoReader.cs (.../WmtsConnectionInfoReader.cs) (revision f427e6dbf1ae2c7855a72a2f2ce9f458ed6167ba) @@ -41,7 +41,6 @@ public class WmtsConnectionInfoReader { private static readonly ILog log = LogManager.GetLogger(typeof(WmtsConnectionInfoReader)); - private string filePath; /// /// Reads the WMTS Connection info objects from . @@ -59,24 +58,23 @@ /// public ReadOnlyCollection ReadWmtsConnectionInfos(string path) { - filePath = path; - IOUtils.ValidateFilePath(filePath); + IOUtils.ValidateFilePath(path); var readConnectionInfos = new WmtsConnectionInfo[0]; - if (!File.Exists(filePath)) + if (!File.Exists(path)) { return new ReadOnlyCollection(readConnectionInfos); } try { - readConnectionInfos = ReadWmtsConnectionInfos().ToArray(); + readConnectionInfos = ReadWmtsConnectionInfosFromFile(path).ToArray(); } catch (Exception exception) when (exception is XmlException || exception is InvalidOperationException || exception is IOException) { - string message = new FileReaderErrorMessageBuilder(filePath) + string message = new FileReaderErrorMessageBuilder(path) .Build(CoreCommonUtilsResources.Error_General_IO_Import_ErrorMessage); throw new CriticalFileReadException(message, exception); } @@ -91,49 +89,64 @@ { using (XmlReader reader = XmlReader.Create(new StringReader(Resources.defaultWmtsConnectionInfo))) { - IList connectionInfos = ReadWmtsConnectionInfos(reader).ToList(); + var connectionInfos = new List(); + while (reader.Read()) + { + if (IsReadElementWmtsConnectionElement(reader)) + { + continue; + } + + WmtsConnectionInfo readWmtsConnectionElement; + using (XmlReader subtreeReader = reader.ReadSubtree()) + { + XElement wmtsConnectionElement = XElement.Load(subtreeReader); + + readWmtsConnectionElement = CreateWmtsConnectionInfo(wmtsConnectionElement); + } + if (readWmtsConnectionElement != null) + { + connectionInfos.Add(readWmtsConnectionElement); + } + } return new ReadOnlyCollection(connectionInfos); } } /// - /// Reads the collection of from . + /// Reads the collection of from . /// + /// The file path that contains the information. /// The read collection. /// Thrown when an error occurred while parsing the XML. /// Thrown when an error occurred while reading the XML. - private IEnumerable ReadWmtsConnectionInfos() + private static IEnumerable ReadWmtsConnectionInfosFromFile(string path) { - using (XmlReader reader = XmlReader.Create(filePath)) + using (XmlReader reader = XmlReader.Create(path)) { - return ReadWmtsConnectionInfos(reader); - } - } - - /// - /// Reads the objects from the . - /// - /// The reader to use. - /// The read WMTS connection infos. - /// Thrown when an error occurred while parsing the XML. - /// Thrown when an error occurred while reading the XML. - private IEnumerable ReadWmtsConnectionInfos(XmlReader reader) - { - var connectionInfos = new List(); - while (reader.Read()) - { - if (IsReadElementWmtsConnectionElement(reader)) + var connectionInfos = new List(); + while (reader.Read()) { - continue; - } + if (IsReadElementWmtsConnectionElement(reader)) + { + continue; + } - WmtsConnectionInfo readWmtsConnectionElement = ReadWmtsConnectionElement(reader); - if (readWmtsConnectionElement != null) - { - connectionInfos.Add(readWmtsConnectionElement); + WmtsConnectionInfo readWmtsConnectionElement; + using (XmlReader subtreeReader = reader.ReadSubtree()) + { + XElement wmtsConnectionElement = XElement.Load(subtreeReader); + + readWmtsConnectionElement = TryCreateWmtsConnectionInfo(path, wmtsConnectionElement); + } + + if (readWmtsConnectionElement != null) + { + connectionInfos.Add(readWmtsConnectionElement); + } } + return connectionInfos; } - return connectionInfos; } /// @@ -149,25 +162,8 @@ || reader.Name != WmtsConnectionInfoXmlDefinitions.WmtsConnectionElement; } - /// - /// Reads a single from . - /// - /// The reader to use. - /// The read or null if none read. - /// Thrown when the - /// is not positioned on an element when this method is called. - private WmtsConnectionInfo ReadWmtsConnectionElement(XmlReader reader) + private static WmtsConnectionInfo TryCreateWmtsConnectionInfo(string path, XContainer element) { - using (XmlReader subtreeReader = reader.ReadSubtree()) - { - XElement wmtsConnectionElement = XElement.Load(subtreeReader); - - return TryCreateWmtsConnectionInfo(wmtsConnectionElement); - } - } - - private WmtsConnectionInfo TryCreateWmtsConnectionInfo(XContainer element) - { XElement nameElement = element.Element(WmtsConnectionInfoXmlDefinitions.WmtsConnectionNameElement); XElement urlElement = element.Element(WmtsConnectionInfoXmlDefinitions.WmtsConnectionUrlElement); @@ -176,23 +172,28 @@ return null; } - return TryCreateWmtsConnectionInfo(nameElement.Value, urlElement.Value); - } - - private WmtsConnectionInfo TryCreateWmtsConnectionInfo(string name, string url) - { try { - return new WmtsConnectionInfo(name, url); + return new WmtsConnectionInfo(nameElement.Value, urlElement.Value); } catch (ArgumentException exception) { - string errorMessage = string.Format(Resources.WmtsConnectionInfoReader_Unable_To_Create_WmtsConnectionInfo, name, url); - string message = new FileReaderErrorMessageBuilder(filePath).Build(errorMessage); + string errorMessage = string.Format(Resources.WmtsConnectionInfoReader_Unable_To_Create_WmtsConnectionInfo, + nameElement.Value, + urlElement.Value); + string message = new FileReaderErrorMessageBuilder(path).Build(errorMessage); log.Warn(message, exception); } return null; } + + private static WmtsConnectionInfo CreateWmtsConnectionInfo(XContainer element) + { + XElement nameElement = element.Element(WmtsConnectionInfoXmlDefinitions.WmtsConnectionNameElement); + XElement urlElement = element.Element(WmtsConnectionInfoXmlDefinitions.WmtsConnectionUrlElement); + + return new WmtsConnectionInfo(nameElement?.Value, urlElement?.Value); + } } } \ No newline at end of file