~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/Misc/ResourceToolkit/Project/Src/ResourceFileContent/ResourceFileContentRegistry.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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)
 
3
 
 
4
using System;
 
5
using System.Collections.Generic;
 
6
using ICSharpCode.Core;
 
7
 
 
8
namespace Hornung.ResourceToolkit.ResourceFileContent
 
9
{
 
10
        /// <summary>
 
11
        /// Provides facilities to load and cache the contents of resource files.
 
12
        /// </summary>
 
13
        public static class ResourceFileContentRegistry
 
14
        {
 
15
                /// <summary>
 
16
                /// The AddIn tree path where the resource file content factories are registered.
 
17
                /// </summary>
 
18
                public const string ResourceFileContentFactoriesAddInTreePath = "/AddIns/ResourceToolkit/ResourceFileContentFactories";
 
19
                
 
20
                static List<IResourceFileContentFactory> factories;
 
21
                
 
22
                /// <summary>
 
23
                /// Gets a list of all registered resource file content factories.
 
24
                /// </summary>
 
25
                public static IEnumerable<IResourceFileContentFactory> Factories {
 
26
                        get {
 
27
                                if (factories == null) {
 
28
                                        factories = AddInTree.BuildItems<IResourceFileContentFactory>(ResourceFileContentFactoriesAddInTreePath, null, false);
 
29
                                }
 
30
                                return factories;
 
31
                        }
 
32
                }
 
33
                
 
34
                
 
35
                static Dictionary<string, IResourceFileContent> resourceFileContents = new Dictionary<string, IResourceFileContent>();
 
36
                
 
37
                /// <summary>
 
38
                /// Gets the resource content for the specified file.
 
39
                /// </summary>
 
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)
 
43
                {
 
44
                        IResourceFileContent c;
 
45
                        if (!resourceFileContents.TryGetValue(fileName, out c)) {
 
46
                                c = CreateResourceFileContent(fileName);
 
47
                                if (c == null) {
 
48
                                        return null;
 
49
                                }
 
50
                                resourceFileContents[fileName] = c;
 
51
                        }
 
52
                        return c;
 
53
                }
 
54
                
 
55
                /// <summary>
 
56
                /// Creates the resource content for the specified file.
 
57
                /// </summary>
 
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)
 
61
                {
 
62
                        IResourceFileContentFactory factory = GetResourceFileContentFactory(fileName);
 
63
                        if (factory == null) {
 
64
                                return null;
 
65
                        } else {
 
66
                                return factory.CreateContentForFile(fileName);
 
67
                        }
 
68
                }
 
69
                
 
70
                /// <summary>
 
71
                /// Gets a <see cref="IResourceFileContentFactory"/> that can create the resource file content
 
72
                /// for the specified resource file.
 
73
                /// </summary>
 
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)
 
77
                {
 
78
                        foreach (IResourceFileContentFactory factory in Factories) {
 
79
                                if (factory.CanCreateContentForFile(fileName)) {
 
80
                                        return factory;
 
81
                                }
 
82
                        }
 
83
                        return null;
 
84
                }
 
85
                
 
86
                // ********************************************************************************************************************************
 
87
                
 
88
                /// <summary>
 
89
                /// The AddIn tree path where the localized resource finders are registered.
 
90
                /// </summary>
 
91
                public const string LocalizedResourcesFindersAddInTreePath = "/AddIns/ResourceToolkit/LocalizedResourcesFinders";
 
92
                
 
93
                static List<ILocalizedResourcesFinder> localizedResourcesFinders;
 
94
                
 
95
                /// <summary>
 
96
                /// Gets a list of all registered localized resources finders.
 
97
                /// </summary>
 
98
                public static IEnumerable<ILocalizedResourcesFinder> LocalizedResourcesFinders {
 
99
                        get {
 
100
                                if (localizedResourcesFinders == null) {
 
101
                                        localizedResourcesFinders = AddInTree.BuildItems<ILocalizedResourcesFinder>(LocalizedResourcesFindersAddInTreePath, null, false);
 
102
                                }
 
103
                                return localizedResourcesFinders;
 
104
                        }
 
105
                }
 
106
                
 
107
                /// <summary>
 
108
                /// Gets localized resources that belong to the master resource file.
 
109
                /// </summary>
 
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)
 
113
                {
 
114
                        Dictionary<string, IResourceFileContent> list = new Dictionary<string, IResourceFileContent>();
 
115
                        foreach (ILocalizedResourcesFinder finder in LocalizedResourcesFinders) {
 
116
                                IDictionary<string, IResourceFileContent> l = finder.GetLocalizedContents(fileName);
 
117
                                if (l != null) {
 
118
                                        foreach (KeyValuePair<string, IResourceFileContent> entry in l) {
 
119
                                                if (!list.ContainsKey(entry.Key)) {
 
120
                                                        list.Add(entry.Key, entry.Value);
 
121
                                                }
 
122
                                        }
 
123
                                }
 
124
                        }
 
125
                        return list;
 
126
                }
 
127
                
 
128
        }
 
129
}