2017-07-06 10:56:03 +02:00
|
|
|
|
/*
|
2018-01-16 08:24:00 +01:00
|
|
|
|
* Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET)
|
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;
|
2017-07-06 18:18:39 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.I18n;
|
2017-07-06 10:56:03 +02:00
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.Core.I18n
|
|
|
|
|
{
|
2018-03-06 14:35:10 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Default implementation of <see cref="ITextResource"/> to load text data from XML files.
|
|
|
|
|
/// </summary>
|
2017-07-06 10:56:03 +02:00
|
|
|
|
public class XmlTextResource : ITextResource
|
|
|
|
|
{
|
2017-10-10 10:17:28 +02:00
|
|
|
|
private string path;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new text resource for an XML file located at the specified path.
|
|
|
|
|
/// </summary>
|
2018-03-06 14:35:10 +01:00
|
|
|
|
/// <exception cref="ArgumentException">If the specifed file does not exist.</exception>
|
|
|
|
|
/// <exception cref="ArgumentNullException">If the given path is null.</exception>
|
2017-10-10 10:17:28 +02:00
|
|
|
|
public XmlTextResource(string path)
|
|
|
|
|
{
|
|
|
|
|
if (path == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(path));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!File.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("The specified file does not exist!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.path = path;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-03 15:35:22 +02:00
|
|
|
|
public IDictionary<TextKey, string> LoadText()
|
2017-07-06 10:56:03 +02:00
|
|
|
|
{
|
|
|
|
|
var xml = XDocument.Load(path);
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|