2017-07-06 10:56:03 +02:00
|
|
|
|
/*
|
2024-03-05 18:37:42 +01:00
|
|
|
|
* Copyright (c) 2024 ETH Zürich, IT Services
|
2017-07-06 10:56:03 +02:00
|
|
|
|
*
|
|
|
|
|
* 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;
|
2019-08-30 09:55:26 +02:00
|
|
|
|
using SafeExamBrowser.I18n.Contracts;
|
2017-07-06 10:56:03 +02:00
|
|
|
|
|
2018-08-31 10:06:27 +02:00
|
|
|
|
namespace SafeExamBrowser.I18n
|
2017-07-06 10:56:03 +02:00
|
|
|
|
{
|
2018-03-06 14:35:10 +01:00
|
|
|
|
/// <summary>
|
2020-03-09 17:35:48 +01:00
|
|
|
|
/// Default implementation of an <see cref="ITextResource"/> for text data in XML.
|
2018-03-06 14:35:10 +01:00
|
|
|
|
/// </summary>
|
2017-07-06 10:56:03 +02:00
|
|
|
|
public class XmlTextResource : ITextResource
|
|
|
|
|
{
|
2020-03-09 17:35:48 +01:00
|
|
|
|
private Stream data;
|
2017-10-10 10:17:28 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-03-09 17:35:48 +01:00
|
|
|
|
/// Initializes a new text resource for an XML data stream.
|
2017-10-10 10:17:28 +02:00
|
|
|
|
/// </summary>
|
2020-03-09 17:35:48 +01:00
|
|
|
|
/// <exception cref="ArgumentNullException">If the given stream is null.</exception>
|
|
|
|
|
public XmlTextResource(Stream data)
|
2017-10-10 10:17:28 +02:00
|
|
|
|
{
|
2020-03-09 17:35:48 +01:00
|
|
|
|
this.data = data ?? throw new ArgumentNullException(nameof(data));
|
2017-10-10 10:17:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-03 15:35:22 +02:00
|
|
|
|
public IDictionary<TextKey, string> LoadText()
|
2017-07-06 10:56:03 +02:00
|
|
|
|
{
|
2020-03-09 17:35:48 +01:00
|
|
|
|
var xml = XDocument.Load(data);
|
2017-08-03 15:35:22 +02:00
|
|
|
|
var text = new Dictionary<TextKey, string>();
|
2017-07-06 10:56:03 +02:00
|
|
|
|
|
|
|
|
|
foreach (var definition in xml.Root.Descendants())
|
|
|
|
|
{
|
2018-01-19 10:11:13 +01:00
|
|
|
|
var isEntry = definition.Name.LocalName == "Entry";
|
|
|
|
|
var hasValidKey = Enum.TryParse(definition.Attribute("key")?.Value, out TextKey key);
|
|
|
|
|
|
|
|
|
|
if (isEntry && hasValidKey)
|
2017-07-06 10:56:03 +02:00
|
|
|
|
{
|
2018-01-19 10:11:13 +01:00
|
|
|
|
text[key] = definition.Value?.Trim() ?? string.Empty;
|
2017-07-06 10:56:03 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|