Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
vers-one committed May 23, 2023
1 parent 25c0244 commit 88d0cb0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 40 deletions.
4 changes: 4 additions & 0 deletions Source/.editorconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[*]

# Visual Studio Spell Checker configuration
spelling_languages = en-us
spelling_error_severity = error
Expand Down Expand Up @@ -69,3 +70,6 @@ dotnet_diagnostic.SA1633.severity = none

# SA1516: Elements should be separated by blank line
dotnet_diagnostic.SA1516.severity = none

# xUnit1004: Test methods should not be skipped
dotnet_diagnostic.xUnit1004.severity = none
39 changes: 7 additions & 32 deletions Source/VersOne.Epub/Readers/Epub2NcxReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,11 @@ public Epub2NcxReader(EpubReaderOptions? epubReaderOptions = null)
{
return null;
}
EpubManifestItem tocManifestItem = package.Manifest.Items.FirstOrDefault(item => item.Id.CompareOrdinalIgnoreCase(tocId));
if (tocManifestItem == null)
{
EpubManifestItem tocManifestItem = package.Manifest.Items.FirstOrDefault(item => item.Id.CompareOrdinalIgnoreCase(tocId)) ??
throw new Epub2NcxException($"EPUB parsing error: TOC item {tocId} not found in EPUB manifest.");
}
string tocFileEntryPath = ContentPathUtils.Combine(contentDirectoryPath, tocManifestItem.Href);
IZipFileEntry? tocFileEntry = epubFile.GetEntry(tocFileEntryPath);
if (tocFileEntry == null)
{
IZipFileEntry? tocFileEntry = epubFile.GetEntry(tocFileEntryPath) ??
throw new Epub2NcxException($"EPUB parsing error: TOC file {tocFileEntryPath} not found in the EPUB file.");
}
if (tocFileEntry.Length > Int32.MaxValue)
{
throw new Epub2NcxException($"EPUB parsing error: TOC file {tocFileEntryPath} is larger than 2 GB.");
Expand All @@ -48,22 +42,10 @@ public Epub2NcxReader(EpubReaderOptions? epubReaderOptions = null)
containerDocument = await XmlUtils.LoadDocumentAsync(containerStream, epubReaderOptions.XmlReaderOptions).ConfigureAwait(false);
}
XNamespace ncxNamespace = "http://www.daisy.org/z3986/2005/ncx/";
XElement ncxNode = containerDocument.Element(ncxNamespace + "ncx");
if (ncxNode == null)
{
throw new Epub2NcxException("EPUB parsing error: TOC file does not contain ncx element.");
}
XElement headNode = ncxNode.Element(ncxNamespace + "head");
if (headNode == null)
{
throw new Epub2NcxException("EPUB parsing error: TOC file does not contain head element.");
}
XElement ncxNode = containerDocument.Element(ncxNamespace + "ncx") ?? throw new Epub2NcxException("EPUB parsing error: TOC file does not contain ncx element.");
XElement headNode = ncxNode.Element(ncxNamespace + "head") ?? throw new Epub2NcxException("EPUB parsing error: TOC file does not contain head element.");
Epub2NcxHead navigationHead = ReadNavigationHead(headNode);
XElement docTitleNode = ncxNode.Element(ncxNamespace + "docTitle");
if (docTitleNode == null)
{
throw new Epub2NcxException("EPUB parsing error: TOC file does not contain docTitle element.");
}
XElement docTitleNode = ncxNode.Element(ncxNamespace + "docTitle") ?? throw new Epub2NcxException("EPUB parsing error: TOC file does not contain docTitle element.");
string? docTitle = ReadNavigationDocTitle(docTitleNode);
List<string> docAuthors = new();
foreach (XElement docAuthorNode in ncxNode.Elements(ncxNamespace + "docAuthor"))
Expand All @@ -74,11 +56,7 @@ public Epub2NcxReader(EpubReaderOptions? epubReaderOptions = null)
docAuthors.Add(navigationDocAuthor);
}
}
XElement navMapNode = ncxNode.Element(ncxNamespace + "navMap");
if (navMapNode == null)
{
throw new Epub2NcxException("EPUB parsing error: TOC file does not contain navMap element.");
}
XElement navMapNode = ncxNode.Element(ncxNamespace + "navMap") ?? throw new Epub2NcxException("EPUB parsing error: TOC file does not contain navMap element.");
Epub2NcxNavigationMap navMap = ReadNavigationMap(navMapNode, epubReaderOptions.Epub2NcxReaderOptions);
XElement pageListNode = ncxNode.Element(ncxNamespace + "pageList");
Epub2NcxPageList? pageList = null;
Expand Down Expand Up @@ -244,11 +222,8 @@ private static Epub2NcxNavigationMap ReadNavigationMap(XElement navigationMapNod

private static Epub2NcxNavigationLabel ReadNavigationLabel(XElement navigationLabelNode)
{
XElement navigationLabelTextNode = navigationLabelNode.Element(navigationLabelNode.Name.Namespace + "text");
if (navigationLabelTextNode == null)
{
XElement navigationLabelTextNode = navigationLabelNode.Element(navigationLabelNode.Name.Namespace + "text") ??
throw new Epub2NcxException("Incorrect EPUB navigation label: label text element is missing.");
}
string text = navigationLabelTextNode.Value;
return new(text);
}
Expand Down
10 changes: 2 additions & 8 deletions Source/VersOne.Epub/Readers/RootFilePathReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,17 @@ public RootFilePathReader(EpubReaderOptions? epubReaderOptions = null)
public async Task<string> GetRootFilePathAsync(IZipFile epubFile)
{
const string EPUB_CONTAINER_FILE_PATH = "META-INF/container.xml";
IZipFileEntry? containerFileEntry = epubFile.GetEntry(EPUB_CONTAINER_FILE_PATH);
if (containerFileEntry == null)
{
IZipFileEntry? containerFileEntry = epubFile.GetEntry(EPUB_CONTAINER_FILE_PATH) ??
throw new EpubContainerException($"EPUB parsing error: \"{EPUB_CONTAINER_FILE_PATH}\" file not found in the EPUB file.");
}
XDocument containerDocument;
using (Stream containerStream = containerFileEntry.Open())
{
containerDocument = await XmlUtils.LoadDocumentAsync(containerStream, epubReaderOptions.XmlReaderOptions).ConfigureAwait(false);
}
XNamespace cnsNamespace = "urn:oasis:names:tc:opendocument:xmlns:container";
XAttribute? fullPathAttribute =
containerDocument.Element(cnsNamespace + "container")?.Element(cnsNamespace + "rootfiles")?.Element(cnsNamespace + "rootfile")?.Attribute("full-path");
if (fullPathAttribute == null)
{
containerDocument.Element(cnsNamespace + "container")?.Element(cnsNamespace + "rootfiles")?.Element(cnsNamespace + "rootfile")?.Attribute("full-path") ??
throw new EpubContainerException("EPUB parsing error: root file path not found in the EPUB container.");
}
return fullPathAttribute.Value;
}
}
Expand Down

0 comments on commit 88d0cb0

Please sign in to comment.