File size: 13,394 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 files within the EPUB book.
/// </summary>
/// <typeparam name="TLocalContentFile">The type of the content files stored within the <see cref="Local" /> collection.</typeparam>
/// <typeparam name="TRemoteContentFile">The type of the content files stored within the <see cref="Remote" /> collection.</typeparam>
public class EpubContentCollection<TLocalContentFile, TRemoteContentFile>
where TLocalContentFile : EpubLocalContentFile
where TRemoteContentFile : EpubRemoteContentFile
{
private readonly Dictionary<string, TLocalContentFile> localByKey;
private readonly Dictionary<string, TLocalContentFile> localByFilePath;
private readonly Dictionary<string, TRemoteContentFile> remoteByUrl;
/// <summary>
/// Initializes a new instance of the <see cref="EpubContentCollection{TLocalContentFile, TRemoteContentFile}" /> class.
/// </summary>
/// <param name="local">Local content files to be stored within this container.</param>
/// <param name="remote">Remote content files to be stored within this container.</param>
public EpubContentCollection(ReadOnlyCollection<TLocalContentFile>? local = null, ReadOnlyCollection<TRemoteContentFile>? remote = null)
{
Local = local ?? new ReadOnlyCollection<TLocalContentFile>(new List<TLocalContentFile>());
Remote = remote ?? new ReadOnlyCollection<TRemoteContentFile>(new List<TRemoteContentFile>());
localByKey = new Dictionary<string, TLocalContentFile>();
localByFilePath = new Dictionary<string, TLocalContentFile>();
foreach (TLocalContentFile localContentFile in Local)
{
if (localByKey.ContainsKey(localContentFile.Key))
{
throw new EpubPackageException($"Incorrect EPUB manifest: item with href = \"{localContentFile.Key}\" is not unique.");
}
localByKey.Add(localContentFile.Key, localContentFile);
if (localByFilePath.ContainsKey(localContentFile.FilePath))
{
throw new EpubPackageException($"Incorrect EPUB manifest: item with absolute file path = \"{localContentFile.FilePath}\" is not unique.");
}
localByFilePath.Add(localContentFile.FilePath, localContentFile);
}
remoteByUrl = new Dictionary<string, TRemoteContentFile>();
foreach (TRemoteContentFile remoteContentFile in Remote)
{
if (remoteByUrl.ContainsKey(remoteContentFile.Url))
{
throw new EpubPackageException($"Incorrect EPUB manifest: item with href = \"{remoteContentFile.Url}\" is not unique.");
}
remoteByUrl.Add(remoteContentFile.Url, remoteContentFile);
}
}
/// <summary>
/// Gets a collection of local content files stored within this container.
/// </summary>
public ReadOnlyCollection<TLocalContentFile> Local { get; }
/// <summary>
/// Gets a collection of remote content files stored within this container.
/// </summary>
public ReadOnlyCollection<TRemoteContentFile> Remote { get; }
/// <summary>
/// Determines whether a local content file with the specified <see cref="EpubContentFile.Key" /> value exists in this container.
/// </summary>
/// <param name="key">The <see cref="EpubContentFile.Key" /> value of the local content file to locate in this container.</param>
/// <returns>
/// <c>true</c> if the local content file with the specified <see cref="EpubContentFile.Key" /> value exists in this container; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="key" /> is <c>null</c>.</exception>
public bool ContainsLocalFileWithKey(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return localByKey.ContainsKey(key);
}
/// <summary>
/// Gets the local content file with the specified <see cref="EpubContentFile.Key" /> value.
/// </summary>
/// <param name="key">The <see cref="EpubContentFile.Key" /> of the local content file to get.</param>
/// <returns>The local content file with the specified <see cref="EpubContentFile.Key" /> value.</returns>
/// <exception cref="ArgumentNullException"><paramref name="key" /> is <c>null</c>.</exception>
/// <exception cref="EpubContentCollectionException">
/// Local content file with the specified <see cref="EpubContentFile.Key" /> value does not exist in this container.
/// </exception>
public TLocalContentFile GetLocalFileByKey(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
try
{
return localByKey[key];
}
catch (KeyNotFoundException)
{
throw new EpubContentCollectionException($"Local content file with key = \"{key}\" does not exist in this container.");
}
}
/// <summary>
/// Gets the local content file with the specified <see cref="EpubContentFile.Key" /> value.
/// </summary>
/// <param name="key">The <see cref="EpubContentFile.Key" /> of the local content file to get.</param>
/// <param name="localContentFile">
/// When this method returns, contains the local content file with the specified <see cref="EpubContentFile.Key" /> value,
/// if such local content file exists in the container; otherwise, <c>null</c>.</param>
/// <returns>
/// <c>true</c> if the local content file with the specified <see cref="EpubContentFile.Key" /> value exists in this container; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="key" /> is <c>null</c>.</exception>
public bool TryGetLocalFileByKey(string key, out TLocalContentFile localContentFile)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return localByKey.TryGetValue(key, out localContentFile);
}
/// <summary>
/// Determines whether a local content file with the specified <see cref="EpubLocalContentFile.FilePath" /> value exists in this container.
/// </summary>
/// <param name="filePath">The <see cref="EpubLocalContentFile.FilePath" /> value of the local content file to locate in this container.</param>
/// <returns>
/// <c>true</c> if the local content file with the specified <see cref="EpubLocalContentFile.FilePath" /> value exists in this container; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="filePath" /> is <c>null</c>.</exception>
public bool ContainsLocalFileWithFilePath(string filePath)
{
if (filePath == null)
{
throw new ArgumentNullException(nameof(filePath));
}
return localByFilePath.ContainsKey(filePath);
}
/// <summary>
/// Gets the local content file with the specified <see cref="EpubLocalContentFile.FilePath" /> value.
/// </summary>
/// <param name="filePath">The <see cref="EpubLocalContentFile.FilePath" /> of the local content file to get.</param>
/// <returns>The local content file with the specified <see cref="EpubLocalContentFile.FilePath" /> value.</returns>
/// <exception cref="ArgumentNullException"><paramref name="filePath" /> is <c>null</c>.</exception>
/// <exception cref="EpubContentCollectionException">
/// Local content file with the specified <see cref="EpubLocalContentFile.FilePath" /> value does not exist in this container.
/// </exception>
public TLocalContentFile GetLocalFileByFilePath(string filePath)
{
if (filePath == null)
{
throw new ArgumentNullException(nameof(filePath));
}
try
{
return localByFilePath[filePath];
}
catch (KeyNotFoundException)
{
throw new EpubContentCollectionException($"Local content file with file path = \"{filePath}\" does not exist in this container.");
}
}
/// <summary>
/// Gets the local content file with the specified <see cref="EpubLocalContentFile.FilePath" /> value.
/// </summary>
/// <param name="filePath">The <see cref="EpubLocalContentFile.FilePath" /> of the local content file to get.</param>
/// <param name="localContentFile">
/// When this method returns, contains the local content file with the specified <see cref="EpubLocalContentFile.FilePath" /> value,
/// if such local content file exists in the container; otherwise, <c>null</c>.</param>
/// <returns>
/// <c>true</c> if the local content file with the specified <see cref="EpubLocalContentFile.FilePath" /> value exists in this container; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="filePath" /> is <c>null</c>.</exception>
public bool TryGetLocalFileByFilePath(string filePath, out TLocalContentFile localContentFile)
{
if (filePath == null)
{
throw new ArgumentNullException(nameof(filePath));
}
return localByFilePath.TryGetValue(filePath, out localContentFile);
}
/// <summary>
/// Determines whether a remote content file with the specified <see cref="EpubRemoteContentFile.Url" /> value exists in this container.
/// </summary>
/// <param name="url">The <see cref="EpubRemoteContentFile.Url" /> value of the remote content file to locate in this container.</param>
/// <returns>
/// <c>true</c> if the remote content file with the specified <see cref="EpubRemoteContentFile.Url" /> value exists in this container; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="url" /> is <c>null</c>.</exception>
public bool ContainsRemoteFileWithUrl(string url)
{
if (url == null)
{
throw new ArgumentNullException(nameof(url));
}
return remoteByUrl.ContainsKey(url);
}
/// <summary>
/// Gets the remote content file with the specified <see cref="EpubRemoteContentFile.Url" /> value.
/// </summary>
/// <param name="url">The <see cref="EpubRemoteContentFile.Url" /> of the remote content file to get.</param>
/// <returns>The remote content file with the specified <see cref="EpubRemoteContentFile.Url" /> value.</returns>
/// <exception cref="ArgumentNullException"><paramref name="url" /> is <c>null</c>.</exception>
/// <exception cref="EpubContentCollectionException">
/// Remote content file with the specified <see cref="EpubRemoteContentFile.Url" /> value does not exist in this container.
/// </exception>
public TRemoteContentFile GetRemoteFileByUrl(string url)
{
if (url == null)
{
throw new ArgumentNullException(nameof(url));
}
try
{
return remoteByUrl[url];
}
catch (KeyNotFoundException)
{
throw new EpubContentCollectionException($"Remote content file with URL = \"{url}\" does not exist in this container.");
}
}
/// <summary>
/// Gets the remote content file with the specified <see cref="EpubRemoteContentFile.Url" /> value.
/// </summary>
/// <param name="url">The <see cref="EpubRemoteContentFile.Url" /> of the remote content file to get.</param>
/// <param name="remoteContentFile">
/// When this method returns, contains the remote content file with the specified <see cref="EpubRemoteContentFile.Url" /> value,
/// if such remote content file exists in the container; otherwise, <c>null</c>.</param>
/// <returns>
/// <c>true</c> if the remote content file with the specified <see cref="EpubRemoteContentFile.Url" /> value exists in this container; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="url" /> is <c>null</c>.</exception>
public bool TryGetRemoteFileByUrl(string url, out TRemoteContentFile remoteContentFile)
{
if (url == null)
{
throw new ArgumentNullException(nameof(url));
}
return remoteByUrl.TryGetValue(url, out remoteContentFile);
}
}
}
|