forked from Zeugma440/atldotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFactory.cs
136 lines (121 loc) · 4.56 KB
/
Factory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System.Collections.Generic;
namespace ATL
{
/// <summary>
/// Abstract factory for data readers, containing shared methods and members
/// </summary>
public abstract class Factory
{
/// <summary>
/// Represents an unknown generic format
/// </summary>
public static readonly Format UNKNOWN_FORMAT = new Format(-1, "Unknown");
/// <summary>
/// List of all formats supported by this kind of data reader
/// They are indexed by file extension to speed up matching
/// </summary>
protected IDictionary<string, IList<Format>> formatListByExt;
/// <summary>
/// List of all formats supported by this kind of data reader
/// They are indexed by MIME-type to speed up matching
/// </summary>
protected IDictionary<string, IList<Format>> formatListByMime;
/// <summary>
/// Adds a format to the supported formats
/// </summary>
/// <param name="f">Format to be added</param>
protected void addFormat(Format f)
{
IList<Format> matchingFormats;
foreach (string ext in f)
{
if (!formatListByExt.ContainsKey(ext))
{
matchingFormats = new List<Format>();
matchingFormats.Add(f);
formatListByExt.Add(ext, matchingFormats);
}
else
{
matchingFormats = formatListByExt[ext];
matchingFormats.Add(f);
}
}
foreach (string mimeType in f.MimeList)
{
if (!formatListByMime.ContainsKey(mimeType))
{
matchingFormats = new List<Format>();
matchingFormats.Add(f);
formatListByMime.Add(mimeType, matchingFormats);
}
else
{
matchingFormats = formatListByMime[mimeType];
matchingFormats.Add(f);
}
}
}
/// <summary>
/// Gets the valid formats from the given file path, using the file extension as key
/// </summary>
/// <param name="path">Path of the file which format to recognize</param>
/// <returns>List of the valid formats matching the extension of the given file,
/// or null if none recognized or the file does not exist</returns>
public IList<Format> getFormatsFromPath(string path)
{
IList<Format> result = null;
string extension;
if (path.Contains("."))
extension = path.Substring(path.LastIndexOf('.'), path.Length - path.LastIndexOf('.')).ToLower();
else
extension = path;
if (formatListByExt.ContainsKey(extension))
{
IList<Format> formats = formatListByExt[extension];
if (formats != null && formats.Count > 0)
{
result = formats;
}
}
return result;
}
/// <summary>
/// Gets the valid formats from the given MIME-type
/// </summary>
/// <param name="mimeType">MIME-type to recognize</param>
/// <returns>List of the valid formats matching the MIME-type of the given file,
/// or null if none recognized</returns>
public IList<Format> getFormatsFromMimeType(string mimeType)
{
IList<Format> result = null;
string mime = mimeType.ToLower();
if (formatListByMime.ContainsKey(mime))
{
IList<Format> formats = formatListByMime[mime];
if (formats != null && formats.Count > 0)
{
result = formats;
}
}
return result;
}
/// <summary>
/// Gets a list of all supported formats
/// </summary>
/// <returns>List of all supported formats</returns>
public ICollection<Format> getFormats()
{
Dictionary<int, Format> result = new Dictionary<int, Format>();
foreach (IList<Format> formats in formatListByExt.Values)
{
foreach (Format f in formats)
{
// Filter duplicates "caused by" indexing formats by extension
if (!result.ContainsKey(f.ID)) result.Add(f.ID, f);
}
}
return result.Values;
}
}
}