From c864fa512cacb0e09c8aa74f9158494a60b9547f Mon Sep 17 00:00:00 2001 From: Eliot Jones Date: Fri, 28 Feb 2020 11:50:18 +0000 Subject: [PATCH] remove islenientparsing from page classes --- .../AdvancedPdfDocumentAccess.cs | 8 ++----- src/UglyToad.PdfPig/Content/IPageFactory.cs | 3 +-- src/UglyToad.PdfPig/Content/Pages.cs | 7 ++---- .../Outline/BookmarksProvider.cs | 23 +++++-------------- src/UglyToad.PdfPig/Parser/PageFactory.cs | 3 +-- .../Parser/Parts/NameTreeParser.cs | 11 ++------- .../Parser/PdfDocumentFactory.cs | 4 ++-- src/UglyToad.PdfPig/PdfDocument.cs | 10 +++----- 8 files changed, 19 insertions(+), 50 deletions(-) diff --git a/src/UglyToad.PdfPig/AdvancedPdfDocumentAccess.cs b/src/UglyToad.PdfPig/AdvancedPdfDocumentAccess.cs index 60c0b643..7c786144 100644 --- a/src/UglyToad.PdfPig/AdvancedPdfDocumentAccess.cs +++ b/src/UglyToad.PdfPig/AdvancedPdfDocumentAccess.cs @@ -17,19 +17,16 @@ private readonly IPdfTokenScanner pdfScanner; private readonly IFilterProvider filterProvider; private readonly Catalog catalog; - private readonly bool isLenientParsing; private bool isDisposed; internal AdvancedPdfDocumentAccess(IPdfTokenScanner pdfScanner, IFilterProvider filterProvider, - Catalog catalog, - bool isLenientParsing) + Catalog catalog) { this.pdfScanner = pdfScanner ?? throw new ArgumentNullException(nameof(pdfScanner)); this.filterProvider = filterProvider ?? throw new ArgumentNullException(nameof(filterProvider)); this.catalog = catalog ?? throw new ArgumentNullException(nameof(catalog)); - this.isLenientParsing = isLenientParsing; } /// @@ -51,8 +48,7 @@ return false; } - var embeddedFileNames = NameTreeParser.FlattenNameTreeToDictionary(embeddedFileNamesDictionary, pdfScanner, isLenientParsing, - x => x); + var embeddedFileNames = NameTreeParser.FlattenNameTreeToDictionary(embeddedFileNamesDictionary, pdfScanner, x => x); if (embeddedFileNames.Count == 0) { diff --git a/src/UglyToad.PdfPig/Content/IPageFactory.cs b/src/UglyToad.PdfPig/Content/IPageFactory.cs index 9076cce1..17c01447 100644 --- a/src/UglyToad.PdfPig/Content/IPageFactory.cs +++ b/src/UglyToad.PdfPig/Content/IPageFactory.cs @@ -4,7 +4,6 @@ internal interface IPageFactory { - Page Create(int number, DictionaryToken dictionary, PageTreeMembers pageTreeMembers, - bool isLenientParsing); + Page Create(int number, DictionaryToken dictionary, PageTreeMembers pageTreeMembers); } } \ No newline at end of file diff --git a/src/UglyToad.PdfPig/Content/Pages.cs b/src/UglyToad.PdfPig/Content/Pages.cs index f6a216ac..10c976c3 100644 --- a/src/UglyToad.PdfPig/Content/Pages.cs +++ b/src/UglyToad.PdfPig/Content/Pages.cs @@ -10,17 +10,14 @@ { private readonly Catalog catalog; private readonly IPageFactory pageFactory; - private readonly bool isLenientParsing; private readonly IPdfTokenScanner pdfScanner; public int Count { get; } - internal Pages(Catalog catalog, IPageFactory pageFactory, bool isLenientParsing, - IPdfTokenScanner pdfScanner) + internal Pages(Catalog catalog, IPageFactory pageFactory, IPdfTokenScanner pdfScanner) { this.catalog = catalog ?? throw new ArgumentNullException(nameof(catalog)); this.pageFactory = pageFactory ?? throw new ArgumentNullException(nameof(pageFactory)); - this.isLenientParsing = isLenientParsing; this.pdfScanner = pdfScanner ?? throw new ArgumentNullException(nameof(pdfScanner)); Count = catalog.PagesDictionary.GetIntOrDefault(NameToken.Count); @@ -66,7 +63,7 @@ } } - var page = pageFactory.Create(pageNumber, pageNode.NodeDictionary, pageTreeMembers, isLenientParsing); + var page = pageFactory.Create(pageNumber, pageNode.NodeDictionary, pageTreeMembers); return page; } diff --git a/src/UglyToad.PdfPig/Outline/BookmarksProvider.cs b/src/UglyToad.PdfPig/Outline/BookmarksProvider.cs index ec818fca..a8609905 100644 --- a/src/UglyToad.PdfPig/Outline/BookmarksProvider.cs +++ b/src/UglyToad.PdfPig/Outline/BookmarksProvider.cs @@ -14,13 +14,11 @@ { private readonly ILog log; private readonly IPdfTokenScanner pdfScanner; - private readonly bool isLenientParsing; - public BookmarksProvider(ILog log, IPdfTokenScanner pdfScanner, bool isLenientParsing) + public BookmarksProvider(ILog log, IPdfTokenScanner pdfScanner) { this.log = log; this.pdfScanner = pdfScanner; - this.isLenientParsing = isLenientParsing; } /// @@ -33,10 +31,9 @@ return null; } - if (!isLenientParsing && outlinesDictionary.TryGet(NameToken.Type, pdfScanner, out NameToken typeName) - && typeName != NameToken.Outlines) + if (outlinesDictionary.TryGet(NameToken.Type, pdfScanner, out NameToken typeName) && typeName != NameToken.Outlines) { - throw new PdfDocumentFormatException($"Outlines (bookmarks) dictionary did not have correct type specified: {typeName}."); + log?.Error($"Outlines (bookmarks) dictionary did not have correct type specified: {typeName}."); } if (!outlinesDictionary.TryGet(NameToken.First, pdfScanner, out DictionaryToken next)) @@ -44,7 +41,7 @@ return null; } - var namedDestinations = ReadNamedDestinations(catalog, pdfScanner, isLenientParsing, log); + var namedDestinations = ReadNamedDestinations(catalog, pdfScanner, log); var roots = new List(); var seen = new HashSet(); @@ -102,10 +99,6 @@ { bookmark = new DocumentBookmarkNode(title, level, destination, children); } - else if (!isLenientParsing) - { - throw new PdfDocumentFormatException($"Invalid destination name for bookmark node: {destStringToken.Data}."); - } else { return; @@ -122,10 +115,6 @@ { bookmark = new DocumentBookmarkNode(title, level, actionResult.destination, children); } - else if (!isLenientParsing) - { - throw new PdfDocumentFormatException($"Invalid action for bookmark node: {actionDictionary}."); - } else { return; @@ -168,7 +157,7 @@ #region Named Destinations private static IReadOnlyDictionary ReadNamedDestinations(Catalog catalog, IPdfTokenScanner pdfScanner, - bool isLenientParsing, ILog log) + ILog log) { var result = new Dictionary(); @@ -198,7 +187,7 @@ * The keys in the name tree may be treated as text strings for display purposes. * The destination value associated with a key in the name tree may be either an array or a dictionary. */ - NameTreeParser.FlattenNameTree(dests, pdfScanner, isLenientParsing, value => + NameTreeParser.FlattenNameTree(dests, pdfScanner, value => { if (TryReadExplicitDestination(value, catalog, pdfScanner, log, out var destination)) { diff --git a/src/UglyToad.PdfPig/Parser/PageFactory.cs b/src/UglyToad.PdfPig/Parser/PageFactory.cs index 22b839c0..2c48f8bf 100644 --- a/src/UglyToad.PdfPig/Parser/PageFactory.cs +++ b/src/UglyToad.PdfPig/Parser/PageFactory.cs @@ -33,8 +33,7 @@ this.pdfScanner = pdfScanner; } - public Page Create(int number, DictionaryToken dictionary, PageTreeMembers pageTreeMembers, - bool isLenientParsing) + public Page Create(int number, DictionaryToken dictionary, PageTreeMembers pageTreeMembers) { if (dictionary == null) { diff --git a/src/UglyToad.PdfPig/Parser/Parts/NameTreeParser.cs b/src/UglyToad.PdfPig/Parser/Parts/NameTreeParser.cs index b5d56aa3..31c926be 100644 --- a/src/UglyToad.PdfPig/Parser/Parts/NameTreeParser.cs +++ b/src/UglyToad.PdfPig/Parser/Parts/NameTreeParser.cs @@ -2,7 +2,6 @@ { using System; using System.Collections.Generic; - using Core; using Tokenization.Scanner; using Tokens; @@ -10,19 +9,17 @@ { public static IReadOnlyDictionary FlattenNameTreeToDictionary(DictionaryToken nameTreeNodeDictionary, IPdfTokenScanner pdfScanner, - bool isLenientParsing, Func valuesFactory) where TResult : class { var result = new Dictionary(); - FlattenNameTree(nameTreeNodeDictionary, pdfScanner, isLenientParsing, valuesFactory, result); + FlattenNameTree(nameTreeNodeDictionary, pdfScanner, valuesFactory, result); return result; } public static void FlattenNameTree(DictionaryToken nameTreeNodeDictionary, IPdfTokenScanner pdfScanner, - bool isLenientParsing, Func valuesFactory, Dictionary result) where TResult : class { @@ -52,11 +49,7 @@ { if (DirectObjectFinder.TryGet(kid, pdfScanner, out DictionaryToken kidDictionary)) { - FlattenNameTree(kidDictionary, pdfScanner, isLenientParsing, valuesFactory, result); - } - else if (!isLenientParsing) - { - throw new PdfDocumentFormatException($"Invalid kids entry in PDF name tree: {kid} in {kids}."); + FlattenNameTree(kidDictionary, pdfScanner, valuesFactory, result); } } } diff --git a/src/UglyToad.PdfPig/Parser/PdfDocumentFactory.cs b/src/UglyToad.PdfPig/Parser/PdfDocumentFactory.cs index b06be55a..0c785480 100644 --- a/src/UglyToad.PdfPig/Parser/PdfDocumentFactory.cs +++ b/src/UglyToad.PdfPig/Parser/PdfDocumentFactory.cs @@ -147,9 +147,9 @@ var caching = new ParsingCachingProviders(bruteForceSearcher, resourceContainer); var acroFormFactory = new AcroFormFactory(pdfScanner, filterProvider, crossReferenceTable); - var bookmarksProvider = new BookmarksProvider(log, pdfScanner, isLenientParsing); + var bookmarksProvider = new BookmarksProvider(log, pdfScanner); - return new PdfDocument(log, inputBytes, version, crossReferenceTable, isLenientParsing, caching, pageFactory, catalog, information, + return new PdfDocument(log, inputBytes, version, crossReferenceTable, caching, pageFactory, catalog, information, encryptionDictionary, pdfScanner, filterProvider, diff --git a/src/UglyToad.PdfPig/PdfDocument.cs b/src/UglyToad.PdfPig/PdfDocument.cs index 57d5c38f..e63299a3 100644 --- a/src/UglyToad.PdfPig/PdfDocument.cs +++ b/src/UglyToad.PdfPig/PdfDocument.cs @@ -25,9 +25,7 @@ { private bool isDisposed; private readonly Lazy documentForm; - - private readonly bool isLenientParsing; - + [NotNull] private readonly HeaderVersion version; @@ -86,7 +84,6 @@ IInputBytes inputBytes, HeaderVersion version, CrossReferenceTable crossReferenceTable, - bool isLenientParsing, ParsingCachingProviders cachingProviders, IPageFactory pageFactory, Catalog catalog, @@ -100,16 +97,15 @@ this.log = log; this.inputBytes = inputBytes; this.version = version ?? throw new ArgumentNullException(nameof(version)); - this.isLenientParsing = isLenientParsing; this.cachingProviders = cachingProviders ?? throw new ArgumentNullException(nameof(cachingProviders)); this.encryptionDictionary = encryptionDictionary; this.pdfScanner = pdfScanner ?? throw new ArgumentNullException(nameof(pdfScanner)); this.filterProvider = filterProvider ?? throw new ArgumentNullException(nameof(filterProvider)); this.bookmarksProvider = bookmarksProvider ?? throw new ArgumentNullException(nameof(bookmarksProvider)); Information = information ?? throw new ArgumentNullException(nameof(information)); - pages = new Pages(catalog, pageFactory, isLenientParsing, pdfScanner); + pages = new Pages(catalog, pageFactory, pdfScanner); Structure = new Structure(catalog, crossReferenceTable, pdfScanner); - Advanced = new AdvancedPdfDocumentAccess(pdfScanner, filterProvider, catalog, isLenientParsing); + Advanced = new AdvancedPdfDocumentAccess(pdfScanner, filterProvider, catalog); documentForm = new Lazy(() => acroFormFactory.GetAcroForm(catalog)); }