File size: 14,172 Bytes
fab29d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace VersOne.Epub
{
    /// <summary>
    /// A container for a subset of content file references within the EPUB book.
    /// </summary>
    /// <typeparam name="TLocalContentFileRef">The type of the content file references stored within the <see cref="Local" /> collection.</typeparam>
    /// <typeparam name="TRemoteContentFileRef">The type of the content file references stored within the <see cref="Remote" /> collection.</typeparam>
    public class EpubContentCollectionRef<TLocalContentFileRef, TRemoteContentFileRef>
        where TLocalContentFileRef : EpubLocalContentFileRef
        where TRemoteContentFileRef : EpubRemoteContentFileRef
    {
        private readonly Dictionary<string, TLocalContentFileRef> localByKey;
        private readonly Dictionary<string, TLocalContentFileRef> localByFilePath;
        private readonly Dictionary<string, TRemoteContentFileRef> remoteByUrl;

        /// <summary>
        /// Initializes a new instance of the <see cref="EpubContentCollectionRef{TLocalContentFileRef, TRemoteContentFileRef}" /> class.
        /// </summary>
        /// <param name="local">Local content file references to be stored within this container.</param>
        /// <param name="remote">Remote content file references to be stored within this container.</param>
        public EpubContentCollectionRef(ReadOnlyCollection<TLocalContentFileRef>? local = null, ReadOnlyCollection<TRemoteContentFileRef>? remote = null)
        {
            Local = local ?? new ReadOnlyCollection<TLocalContentFileRef>(new List<TLocalContentFileRef>());
            Remote = remote ?? new ReadOnlyCollection<TRemoteContentFileRef>(new List<TRemoteContentFileRef>());
            localByKey = new Dictionary<string, TLocalContentFileRef>();
            localByFilePath = new Dictionary<string, TLocalContentFileRef>();
            foreach (TLocalContentFileRef localContentFileRef in Local)
            {
                if (localByKey.ContainsKey(localContentFileRef.Key))
                {
                    throw new EpubPackageException($"Incorrect EPUB manifest: item with href = \"{localContentFileRef.Key}\" is not unique.");
                }
                localByKey.Add(localContentFileRef.Key, localContentFileRef);
                if (localByFilePath.ContainsKey(localContentFileRef.FilePath))
                {
                    throw new EpubPackageException($"Incorrect EPUB manifest: item with absolute file path = \"{localContentFileRef.FilePath}\" is not unique.");
                }
                localByFilePath.Add(localContentFileRef.FilePath, localContentFileRef);
            }
            remoteByUrl = new Dictionary<string, TRemoteContentFileRef>();
            foreach (TRemoteContentFileRef remoteContentFileRef in Remote)
            {
                if (remoteByUrl.ContainsKey(remoteContentFileRef.Url))
                {
                    throw new EpubPackageException($"Incorrect EPUB manifest: item with href = \"{remoteContentFileRef.Url}\" is not unique.");
                }
                remoteByUrl.Add(remoteContentFileRef.Url, remoteContentFileRef);
            }
        }

        /// <summary>
        /// Gets a collection of local content file references stored within this container.
        /// </summary>
        public ReadOnlyCollection<TLocalContentFileRef> Local { get; }

        /// <summary>
        /// Gets a collection of remote content file references stored within this container.
        /// </summary>
        public ReadOnlyCollection<TRemoteContentFileRef> Remote { get; }

        /// <summary>
        /// Determines whether a local content file reference with the specified <see cref="EpubContentFileRef.Key" /> value exists in this container.
        /// </summary>
        /// <param name="key">The <see cref="EpubContentFileRef.Key" /> value of the local content file reference to locate in this container.</param>
        /// <returns>
        /// <c>true</c> if the local content file reference with the specified <see cref="EpubContentFileRef.Key" /> value exists in this container; otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="key" /> is <c>null</c>.</exception>
        public bool ContainsLocalFileRefWithKey(string key)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }
            return localByKey.ContainsKey(key);
        }

        /// <summary>
        /// Gets the local content file reference with the specified <see cref="EpubContentFileRef.Key" /> value.
        /// </summary>
        /// <param name="key">The <see cref="EpubContentFileRef.Key" /> of the local content file reference to get.</param>
        /// <returns>The local content file reference with the specified <see cref="EpubContentFileRef.Key" /> value.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="key" /> is <c>null</c>.</exception>
        /// <exception cref="EpubContentCollectionRefException">
        /// Local content file reference with the specified <see cref="EpubContentFileRef.Key" /> value does not exist in this container.
        /// </exception>
        public TLocalContentFileRef GetLocalFileRefByKey(string key)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }
            try
            {
                return localByKey[key];
            }
            catch (KeyNotFoundException)
            {
                throw new EpubContentCollectionRefException($"Local content file reference with key = \"{key}\" does not exist in this container.");
            }
        }

        /// <summary>
        /// Gets the local content file reference with the specified <see cref="EpubContentFileRef.Key" /> value.
        /// </summary>
        /// <param name="key">The <see cref="EpubContentFileRef.Key" /> of the local content file reference to get.</param>
        /// <param name="localContentFileRef">
        /// When this method returns, contains the local content file reference with the specified <see cref="EpubContentFileRef.Key" /> value,
        /// if such local content file reference exists in the container; otherwise, <c>null</c>.</param>
        /// <returns>
        /// <c>true</c> if the local content file reference with the specified <see cref="EpubContentFileRef.Key" /> value exists in this container; otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="key" /> is <c>null</c>.</exception>
        public bool TryGetLocalFileRefByKey(string key, out TLocalContentFileRef localContentFileRef)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }
            return localByKey.TryGetValue(key, out localContentFileRef);
        }

        /// <summary>
        /// Determines whether a local content file reference with the specified <see cref="EpubLocalContentFileRef.FilePath" /> value exists in this container.
        /// </summary>
        /// <param name="filePath">The <see cref="EpubLocalContentFileRef.FilePath" /> value of the local content file reference to locate in this container.</param>
        /// <returns>
        /// <c>true</c> if the local content file reference with the specified <see cref="EpubLocalContentFileRef.FilePath" /> value exists in this container; otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="filePath" /> is <c>null</c>.</exception>
        public bool ContainsLocalFileRefWithFilePath(string filePath)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException(nameof(filePath));
            }
            return localByFilePath.ContainsKey(filePath);
        }

        /// <summary>
        /// Gets the local content file reference with the specified <see cref="EpubLocalContentFileRef.FilePath" /> value.
        /// </summary>
        /// <param name="filePath">The <see cref="EpubLocalContentFileRef.FilePath" /> of the local content file reference to get.</param>
        /// <returns>The local content file reference with the specified <see cref="EpubLocalContentFileRef.FilePath" /> value.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="filePath" /> is <c>null</c>.</exception>
        /// <exception cref="EpubContentCollectionRefException">
        /// Local content file reference with the specified <see cref="EpubLocalContentFileRef.FilePath" /> value does not exist in this container.
        /// </exception>
        public TLocalContentFileRef GetLocalFileRefByFilePath(string filePath)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException(nameof(filePath));
            }
            try
            {
                return localByFilePath[filePath];
            }
            catch (KeyNotFoundException)
            {
                throw new EpubContentCollectionRefException($"Local content file reference with file path = \"{filePath}\" does not exist in this container.");
            }
        }

        /// <summary>
        /// Gets the local content file reference with the specified <see cref="EpubLocalContentFileRef.FilePath" /> value.
        /// </summary>
        /// <param name="filePath">The <see cref="EpubLocalContentFileRef.FilePath" /> of the local content file reference to get.</param>
        /// <param name="localContentFileRef">
        /// When this method returns, contains the local content file reference with the specified <see cref="EpubLocalContentFileRef.FilePath" /> value,
        /// if such local content file reference exists in the container; otherwise, <c>null</c>.</param>
        /// <returns>
        /// <c>true</c> if the local content file reference with the specified <see cref="EpubLocalContentFileRef.FilePath" /> value exists in this container; otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="filePath" /> is <c>null</c>.</exception>
        public bool TryGetLocalFileRefByFilePath(string filePath, out TLocalContentFileRef localContentFileRef)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException(nameof(filePath));
            }
            return localByFilePath.TryGetValue(filePath, out localContentFileRef);
        }

        /// <summary>
        /// Determines whether a remote content file reference with the specified <see cref="EpubRemoteContentFileRef.Url" /> value exists in this container.
        /// </summary>
        /// <param name="url">The <see cref="EpubRemoteContentFileRef.Url" /> value of the remote content file reference to locate in this container.</param>
        /// <returns>
        /// <c>true</c> if the remote content file reference with the specified <see cref="EpubRemoteContentFileRef.Url" /> value exists in this container; otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="url" /> is <c>null</c>.</exception>
        public bool ContainsRemoteFileRefWithUrl(string url)
        {
            if (url == null)
            {
                throw new ArgumentNullException(nameof(url));
            }
            return remoteByUrl.ContainsKey(url);
        }

        /// <summary>
        /// Gets the remote content file reference with the specified <see cref="EpubRemoteContentFileRef.Url" /> value.
        /// </summary>
        /// <param name="url">The <see cref="EpubRemoteContentFileRef.Url" /> of the remote content file reference to get.</param>
        /// <returns>The remote content file reference with the specified <see cref="EpubRemoteContentFileRef.Url" /> value.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="url" /> is <c>null</c>.</exception>
        /// <exception cref="EpubContentCollectionRefException">
        /// Remote content file reference with the specified <see cref="EpubRemoteContentFileRef.Url" /> value does not exist in this container.
        /// </exception>
        public TRemoteContentFileRef GetRemoteFileRefByUrl(string url)
        {
            if (url == null)
            {
                throw new ArgumentNullException(nameof(url));
            }
            try
            {
                return remoteByUrl[url];
            }
            catch (KeyNotFoundException)
            {
                throw new EpubContentCollectionRefException($"Remote content file reference with URL = \"{url}\" does not exist in this container.");
            }
        }

        /// <summary>
        /// Gets the remote content file reference with the specified <see cref="EpubRemoteContentFileRef.Url" /> value.
        /// </summary>
        /// <param name="url">The <see cref="EpubRemoteContentFileRef.Url" /> of the remote content file reference to get.</param>
        /// <param name="remoteContentFileRef">
        /// When this method returns, contains the remote content file reference with the specified <see cref="EpubRemoteContentFileRef.Url" /> value,
        /// if such remote content file reference exists in the container; otherwise, <c>null</c>.</param>
        /// <returns>
        /// <c>true</c> if the remote content file reference with the specified <see cref="EpubRemoteContentFileRef.Url" /> value exists in this container; otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="url" /> is <c>null</c>.</exception>
        public bool TryGetRemoteFileRefByUrl(string url, out TRemoteContentFileRef remoteContentFileRef)
        {
            if (url == null)
            {
                throw new ArgumentNullException(nameof(url));
            }
            return remoteByUrl.TryGetValue(url, out remoteContentFileRef);
        }
    }
}