remove islenientparsing from page classes

This commit is contained in:
Eliot Jones
2020-02-28 11:50:18 +00:00
parent 48d166276d
commit c864fa512c
8 changed files with 19 additions and 50 deletions

View File

@@ -17,19 +17,16 @@
private readonly IPdfTokenScanner pdfScanner; private readonly IPdfTokenScanner pdfScanner;
private readonly IFilterProvider filterProvider; private readonly IFilterProvider filterProvider;
private readonly Catalog catalog; private readonly Catalog catalog;
private readonly bool isLenientParsing;
private bool isDisposed; private bool isDisposed;
internal AdvancedPdfDocumentAccess(IPdfTokenScanner pdfScanner, internal AdvancedPdfDocumentAccess(IPdfTokenScanner pdfScanner,
IFilterProvider filterProvider, IFilterProvider filterProvider,
Catalog catalog, Catalog catalog)
bool isLenientParsing)
{ {
this.pdfScanner = pdfScanner ?? throw new ArgumentNullException(nameof(pdfScanner)); this.pdfScanner = pdfScanner ?? throw new ArgumentNullException(nameof(pdfScanner));
this.filterProvider = filterProvider ?? throw new ArgumentNullException(nameof(filterProvider)); this.filterProvider = filterProvider ?? throw new ArgumentNullException(nameof(filterProvider));
this.catalog = catalog ?? throw new ArgumentNullException(nameof(catalog)); this.catalog = catalog ?? throw new ArgumentNullException(nameof(catalog));
this.isLenientParsing = isLenientParsing;
} }
/// <summary> /// <summary>
@@ -51,8 +48,7 @@
return false; return false;
} }
var embeddedFileNames = NameTreeParser.FlattenNameTreeToDictionary(embeddedFileNamesDictionary, pdfScanner, isLenientParsing, var embeddedFileNames = NameTreeParser.FlattenNameTreeToDictionary(embeddedFileNamesDictionary, pdfScanner, x => x);
x => x);
if (embeddedFileNames.Count == 0) if (embeddedFileNames.Count == 0)
{ {

View File

@@ -4,7 +4,6 @@
internal interface IPageFactory internal interface IPageFactory
{ {
Page Create(int number, DictionaryToken dictionary, PageTreeMembers pageTreeMembers, Page Create(int number, DictionaryToken dictionary, PageTreeMembers pageTreeMembers);
bool isLenientParsing);
} }
} }

View File

@@ -10,17 +10,14 @@
{ {
private readonly Catalog catalog; private readonly Catalog catalog;
private readonly IPageFactory pageFactory; private readonly IPageFactory pageFactory;
private readonly bool isLenientParsing;
private readonly IPdfTokenScanner pdfScanner; private readonly IPdfTokenScanner pdfScanner;
public int Count { get; } public int Count { get; }
internal Pages(Catalog catalog, IPageFactory pageFactory, bool isLenientParsing, internal Pages(Catalog catalog, IPageFactory pageFactory, IPdfTokenScanner pdfScanner)
IPdfTokenScanner pdfScanner)
{ {
this.catalog = catalog ?? throw new ArgumentNullException(nameof(catalog)); this.catalog = catalog ?? throw new ArgumentNullException(nameof(catalog));
this.pageFactory = pageFactory ?? throw new ArgumentNullException(nameof(pageFactory)); this.pageFactory = pageFactory ?? throw new ArgumentNullException(nameof(pageFactory));
this.isLenientParsing = isLenientParsing;
this.pdfScanner = pdfScanner ?? throw new ArgumentNullException(nameof(pdfScanner)); this.pdfScanner = pdfScanner ?? throw new ArgumentNullException(nameof(pdfScanner));
Count = catalog.PagesDictionary.GetIntOrDefault(NameToken.Count); 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; return page;
} }

View File

@@ -14,13 +14,11 @@
{ {
private readonly ILog log; private readonly ILog log;
private readonly IPdfTokenScanner pdfScanner; 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.log = log;
this.pdfScanner = pdfScanner; this.pdfScanner = pdfScanner;
this.isLenientParsing = isLenientParsing;
} }
/// <summary> /// <summary>
@@ -33,10 +31,9 @@
return null; return null;
} }
if (!isLenientParsing && outlinesDictionary.TryGet(NameToken.Type, pdfScanner, out NameToken typeName) if (outlinesDictionary.TryGet(NameToken.Type, pdfScanner, out NameToken typeName) && typeName != NameToken.Outlines)
&& 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)) if (!outlinesDictionary.TryGet(NameToken.First, pdfScanner, out DictionaryToken next))
@@ -44,7 +41,7 @@
return null; return null;
} }
var namedDestinations = ReadNamedDestinations(catalog, pdfScanner, isLenientParsing, log); var namedDestinations = ReadNamedDestinations(catalog, pdfScanner, log);
var roots = new List<BookmarkNode>(); var roots = new List<BookmarkNode>();
var seen = new HashSet<IndirectReference>(); var seen = new HashSet<IndirectReference>();
@@ -102,10 +99,6 @@
{ {
bookmark = new DocumentBookmarkNode(title, level, destination, children); bookmark = new DocumentBookmarkNode(title, level, destination, children);
} }
else if (!isLenientParsing)
{
throw new PdfDocumentFormatException($"Invalid destination name for bookmark node: {destStringToken.Data}.");
}
else else
{ {
return; return;
@@ -122,10 +115,6 @@
{ {
bookmark = new DocumentBookmarkNode(title, level, actionResult.destination, children); bookmark = new DocumentBookmarkNode(title, level, actionResult.destination, children);
} }
else if (!isLenientParsing)
{
throw new PdfDocumentFormatException($"Invalid action for bookmark node: {actionDictionary}.");
}
else else
{ {
return; return;
@@ -168,7 +157,7 @@
#region Named Destinations #region Named Destinations
private static IReadOnlyDictionary<string, ExplicitDestination> ReadNamedDestinations(Catalog catalog, IPdfTokenScanner pdfScanner, private static IReadOnlyDictionary<string, ExplicitDestination> ReadNamedDestinations(Catalog catalog, IPdfTokenScanner pdfScanner,
bool isLenientParsing, ILog log) ILog log)
{ {
var result = new Dictionary<string, ExplicitDestination>(); var result = new Dictionary<string, ExplicitDestination>();
@@ -198,7 +187,7 @@
* The keys in the name tree may be treated as text strings for display purposes. * 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. * 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)) if (TryReadExplicitDestination(value, catalog, pdfScanner, log, out var destination))
{ {

View File

@@ -33,8 +33,7 @@
this.pdfScanner = pdfScanner; this.pdfScanner = pdfScanner;
} }
public Page Create(int number, DictionaryToken dictionary, PageTreeMembers pageTreeMembers, public Page Create(int number, DictionaryToken dictionary, PageTreeMembers pageTreeMembers)
bool isLenientParsing)
{ {
if (dictionary == null) if (dictionary == null)
{ {

View File

@@ -2,7 +2,6 @@
{ {
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Core;
using Tokenization.Scanner; using Tokenization.Scanner;
using Tokens; using Tokens;
@@ -10,19 +9,17 @@
{ {
public static IReadOnlyDictionary<string, TResult> FlattenNameTreeToDictionary<TResult>(DictionaryToken nameTreeNodeDictionary, public static IReadOnlyDictionary<string, TResult> FlattenNameTreeToDictionary<TResult>(DictionaryToken nameTreeNodeDictionary,
IPdfTokenScanner pdfScanner, IPdfTokenScanner pdfScanner,
bool isLenientParsing,
Func<IToken, TResult> valuesFactory) where TResult : class Func<IToken, TResult> valuesFactory) where TResult : class
{ {
var result = new Dictionary<string, TResult>(); var result = new Dictionary<string, TResult>();
FlattenNameTree(nameTreeNodeDictionary, pdfScanner, isLenientParsing, valuesFactory, result); FlattenNameTree(nameTreeNodeDictionary, pdfScanner, valuesFactory, result);
return result; return result;
} }
public static void FlattenNameTree<TResult>(DictionaryToken nameTreeNodeDictionary, public static void FlattenNameTree<TResult>(DictionaryToken nameTreeNodeDictionary,
IPdfTokenScanner pdfScanner, IPdfTokenScanner pdfScanner,
bool isLenientParsing,
Func<IToken, TResult> valuesFactory, Func<IToken, TResult> valuesFactory,
Dictionary<string, TResult> result) where TResult : class Dictionary<string, TResult> result) where TResult : class
{ {
@@ -52,11 +49,7 @@
{ {
if (DirectObjectFinder.TryGet(kid, pdfScanner, out DictionaryToken kidDictionary)) if (DirectObjectFinder.TryGet(kid, pdfScanner, out DictionaryToken kidDictionary))
{ {
FlattenNameTree(kidDictionary, pdfScanner, isLenientParsing, valuesFactory, result); FlattenNameTree(kidDictionary, pdfScanner, valuesFactory, result);
}
else if (!isLenientParsing)
{
throw new PdfDocumentFormatException($"Invalid kids entry in PDF name tree: {kid} in {kids}.");
} }
} }
} }

View File

@@ -147,9 +147,9 @@
var caching = new ParsingCachingProviders(bruteForceSearcher, resourceContainer); var caching = new ParsingCachingProviders(bruteForceSearcher, resourceContainer);
var acroFormFactory = new AcroFormFactory(pdfScanner, filterProvider, crossReferenceTable); 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, encryptionDictionary,
pdfScanner, pdfScanner,
filterProvider, filterProvider,

View File

@@ -26,8 +26,6 @@
private bool isDisposed; private bool isDisposed;
private readonly Lazy<AcroForm> documentForm; private readonly Lazy<AcroForm> documentForm;
private readonly bool isLenientParsing;
[NotNull] [NotNull]
private readonly HeaderVersion version; private readonly HeaderVersion version;
@@ -86,7 +84,6 @@
IInputBytes inputBytes, IInputBytes inputBytes,
HeaderVersion version, HeaderVersion version,
CrossReferenceTable crossReferenceTable, CrossReferenceTable crossReferenceTable,
bool isLenientParsing,
ParsingCachingProviders cachingProviders, ParsingCachingProviders cachingProviders,
IPageFactory pageFactory, IPageFactory pageFactory,
Catalog catalog, Catalog catalog,
@@ -100,16 +97,15 @@
this.log = log; this.log = log;
this.inputBytes = inputBytes; this.inputBytes = inputBytes;
this.version = version ?? throw new ArgumentNullException(nameof(version)); this.version = version ?? throw new ArgumentNullException(nameof(version));
this.isLenientParsing = isLenientParsing;
this.cachingProviders = cachingProviders ?? throw new ArgumentNullException(nameof(cachingProviders)); this.cachingProviders = cachingProviders ?? throw new ArgumentNullException(nameof(cachingProviders));
this.encryptionDictionary = encryptionDictionary; this.encryptionDictionary = encryptionDictionary;
this.pdfScanner = pdfScanner ?? throw new ArgumentNullException(nameof(pdfScanner)); this.pdfScanner = pdfScanner ?? throw new ArgumentNullException(nameof(pdfScanner));
this.filterProvider = filterProvider ?? throw new ArgumentNullException(nameof(filterProvider)); this.filterProvider = filterProvider ?? throw new ArgumentNullException(nameof(filterProvider));
this.bookmarksProvider = bookmarksProvider ?? throw new ArgumentNullException(nameof(bookmarksProvider)); this.bookmarksProvider = bookmarksProvider ?? throw new ArgumentNullException(nameof(bookmarksProvider));
Information = information ?? throw new ArgumentNullException(nameof(information)); 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); Structure = new Structure(catalog, crossReferenceTable, pdfScanner);
Advanced = new AdvancedPdfDocumentAccess(pdfScanner, filterProvider, catalog, isLenientParsing); Advanced = new AdvancedPdfDocumentAccess(pdfScanner, filterProvider, catalog);
documentForm = new Lazy<AcroForm>(() => acroFormFactory.GetAcroForm(catalog)); documentForm = new Lazy<AcroForm>(() => acroFormFactory.GetAcroForm(catalog));
} }