1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
5
using System.Collections.Generic;
6
using ICSharpCode.Core;
8
namespace Hornung.ResourceToolkit.ResourceFileContent
11
/// Provides facilities to load and cache the contents of resource files.
13
public static class ResourceFileContentRegistry
16
/// The AddIn tree path where the resource file content factories are registered.
18
public const string ResourceFileContentFactoriesAddInTreePath = "/AddIns/ResourceToolkit/ResourceFileContentFactories";
20
static List<IResourceFileContentFactory> factories;
23
/// Gets a list of all registered resource file content factories.
25
public static IEnumerable<IResourceFileContentFactory> Factories {
27
if (factories == null) {
28
factories = AddInTree.BuildItems<IResourceFileContentFactory>(ResourceFileContentFactoriesAddInTreePath, null, false);
35
static Dictionary<string, IResourceFileContent> resourceFileContents = new Dictionary<string, IResourceFileContent>();
38
/// Gets the resource content for the specified file.
40
/// <param name="fileName">The name of the file to get a resource content for.</param>
41
/// <returns>The resource content for the specified file, or <c>null</c> if the format of the specified resource file cannot be handled.</returns>
42
public static IResourceFileContent GetResourceFileContent(string fileName)
44
IResourceFileContent c;
45
if (!resourceFileContents.TryGetValue(fileName, out c)) {
46
c = CreateResourceFileContent(fileName);
50
resourceFileContents[fileName] = c;
56
/// Creates the resource content for the specified file.
58
/// <param name="fileName">The name of the file to create a resource content for.</param>
59
/// <returns>The resource content for the specified file, or <c>null</c>, if the resource file format cannot be handled.</returns>
60
static IResourceFileContent CreateResourceFileContent(string fileName)
62
IResourceFileContentFactory factory = GetResourceFileContentFactory(fileName);
63
if (factory == null) {
66
return factory.CreateContentForFile(fileName);
71
/// Gets a <see cref="IResourceFileContentFactory"/> that can create the resource file content
72
/// for the specified resource file.
74
/// <param name="fileName">The resource file to get a <see cref="IResourceFileContentFactory"/> for.</param>
75
/// <returns>A <see cref="IResourceFileContentFactory"/> that can create the resource file content for the specified file, or <c>null</c> if the specified file is not supported by any registered resource file content factory.</returns>
76
public static IResourceFileContentFactory GetResourceFileContentFactory(string fileName)
78
foreach (IResourceFileContentFactory factory in Factories) {
79
if (factory.CanCreateContentForFile(fileName)) {
86
// ********************************************************************************************************************************
89
/// The AddIn tree path where the localized resource finders are registered.
91
public const string LocalizedResourcesFindersAddInTreePath = "/AddIns/ResourceToolkit/LocalizedResourcesFinders";
93
static List<ILocalizedResourcesFinder> localizedResourcesFinders;
96
/// Gets a list of all registered localized resources finders.
98
public static IEnumerable<ILocalizedResourcesFinder> LocalizedResourcesFinders {
100
if (localizedResourcesFinders == null) {
101
localizedResourcesFinders = AddInTree.BuildItems<ILocalizedResourcesFinder>(LocalizedResourcesFindersAddInTreePath, null, false);
103
return localizedResourcesFinders;
108
/// Gets localized resources that belong to the master resource file.
110
/// <param name="fileName">The name of the master resource file.</param>
111
/// <returns>A dictionary of culture names and associated resource file contents.</returns>
112
public static IDictionary<string, IResourceFileContent> GetLocalizedContents(string fileName)
114
Dictionary<string, IResourceFileContent> list = new Dictionary<string, IResourceFileContent>();
115
foreach (ILocalizedResourcesFinder finder in LocalizedResourcesFinders) {
116
IDictionary<string, IResourceFileContent> l = finder.GetLocalizedContents(fileName);
118
foreach (KeyValuePair<string, IResourceFileContent> entry in l) {
119
if (!list.ContainsKey(entry.Key)) {
120
list.Add(entry.Key, entry.Value);