/* * Copyright (c) 2024 ETH Zürich, IT Services * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ using System; using System.Collections.Generic; using System.IO; using System.Xml.Linq; using SafeExamBrowser.I18n.Contracts; namespace SafeExamBrowser.I18n { /// /// Default implementation of an for text data in XML. /// public class XmlTextResource : ITextResource { private Stream data; /// /// Initializes a new text resource for an XML data stream. /// /// If the given stream is null. public XmlTextResource(Stream data) { this.data = data ?? throw new ArgumentNullException(nameof(data)); } public IDictionary LoadText() { var xml = XDocument.Load(data); var text = new Dictionary(); foreach (var definition in xml.Root.Descendants()) { var isEntry = definition.Name.LocalName == "Entry"; var hasValidKey = Enum.TryParse(definition.Attribute("key")?.Value, out TextKey key); if (isEntry && hasValidKey) { text[key] = definition.Value?.Trim() ?? string.Empty; } } return text; } } }