using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace VersOne.Epub
{
///
/// A container for a subset of content file references within the EPUB book.
///
/// The type of the content file references stored within the collection.
/// The type of the content file references stored within the collection.
public class EpubContentCollectionRef
where TLocalContentFileRef : EpubLocalContentFileRef
where TRemoteContentFileRef : EpubRemoteContentFileRef
{
private readonly Dictionary localByKey;
private readonly Dictionary localByFilePath;
private readonly Dictionary remoteByUrl;
///
/// Initializes a new instance of the class.
///
/// Local content file references to be stored within this container.
/// Remote content file references to be stored within this container.
public EpubContentCollectionRef(ReadOnlyCollection? local = null, ReadOnlyCollection? remote = null)
{
Local = local ?? new ReadOnlyCollection(new List());
Remote = remote ?? new ReadOnlyCollection(new List());
localByKey = new Dictionary();
localByFilePath = new Dictionary();
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();
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);
}
}
///
/// Gets a collection of local content file references stored within this container.
///
public ReadOnlyCollection Local { get; }
///
/// Gets a collection of remote content file references stored within this container.
///
public ReadOnlyCollection Remote { get; }
///
/// Determines whether a local content file reference with the specified value exists in this container.
///
/// The value of the local content file reference to locate in this container.
///
/// true if the local content file reference with the specified value exists in this container; otherwise, false.
///
/// is null.
public bool ContainsLocalFileRefWithKey(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return localByKey.ContainsKey(key);
}
///
/// Gets the local content file reference with the specified value.
///
/// The of the local content file reference to get.
/// The local content file reference with the specified value.
/// is null.
///
/// Local content file reference with the specified value does not exist in this container.
///
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.");
}
}
///
/// Gets the local content file reference with the specified value.
///
/// The of the local content file reference to get.
///
/// When this method returns, contains the local content file reference with the specified value,
/// if such local content file reference exists in the container; otherwise, null.
///
/// true if the local content file reference with the specified value exists in this container; otherwise, false.
///
/// is null.
public bool TryGetLocalFileRefByKey(string key, out TLocalContentFileRef localContentFileRef)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return localByKey.TryGetValue(key, out localContentFileRef);
}
///
/// Determines whether a local content file reference with the specified value exists in this container.
///
/// The value of the local content file reference to locate in this container.
///
/// true if the local content file reference with the specified value exists in this container; otherwise, false.
///
/// is null.
public bool ContainsLocalFileRefWithFilePath(string filePath)
{
if (filePath == null)
{
throw new ArgumentNullException(nameof(filePath));
}
return localByFilePath.ContainsKey(filePath);
}
///
/// Gets the local content file reference with the specified value.
///
/// The of the local content file reference to get.
/// The local content file reference with the specified value.
/// is null.
///
/// Local content file reference with the specified value does not exist in this container.
///
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.");
}
}
///
/// Gets the local content file reference with the specified value.
///
/// The of the local content file reference to get.
///
/// When this method returns, contains the local content file reference with the specified value,
/// if such local content file reference exists in the container; otherwise, null.
///
/// true if the local content file reference with the specified value exists in this container; otherwise, false.
///
/// is null.
public bool TryGetLocalFileRefByFilePath(string filePath, out TLocalContentFileRef localContentFileRef)
{
if (filePath == null)
{
throw new ArgumentNullException(nameof(filePath));
}
return localByFilePath.TryGetValue(filePath, out localContentFileRef);
}
///
/// Determines whether a remote content file reference with the specified value exists in this container.
///
/// The value of the remote content file reference to locate in this container.
///
/// true if the remote content file reference with the specified value exists in this container; otherwise, false.
///
/// is null.
public bool ContainsRemoteFileRefWithUrl(string url)
{
if (url == null)
{
throw new ArgumentNullException(nameof(url));
}
return remoteByUrl.ContainsKey(url);
}
///
/// Gets the remote content file reference with the specified value.
///
/// The of the remote content file reference to get.
/// The remote content file reference with the specified value.
/// is null.
///
/// Remote content file reference with the specified value does not exist in this container.
///
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.");
}
}
///
/// Gets the remote content file reference with the specified value.
///
/// The of the remote content file reference to get.
///
/// When this method returns, contains the remote content file reference with the specified value,
/// if such remote content file reference exists in the container; otherwise, null.
///
/// true if the remote content file reference with the specified value exists in this container; otherwise, false.
///
/// is null.
public bool TryGetRemoteFileRefByUrl(string url, out TRemoteContentFileRef remoteContentFileRef)
{
if (url == null)
{
throw new ArgumentNullException(nameof(url));
}
return remoteByUrl.TryGetValue(url, out remoteContentFileRef);
}
}
}