You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2019/12/29 14:04:16 UTC

svn commit: r1872081 - in /pdfbox/branches/issue4569: pdfbox/src/main/java/org/apache/pdfbox/pdfparser/ preflight/src/main/java/org/apache/pdfbox/preflight/ preflight/src/main/java/org/apache/pdfbox/preflight/graphic/ preflight/src/main/java/org/apache...

Author: lehmi
Date: Sun Dec 29 14:04:16 2019
New Revision: 1872081

URL: http://svn.apache.org/viewvc?rev=1872081&view=rev
Log:
PDFBOX-4569: refactor Preflight, use ondemand parser, add a static method to validate a given pdf

Modified:
    pdfbox/branches/issue4569/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java
    pdfbox/branches/issue4569/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java
    pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/PreflightDocument.java
    pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/Validator_A1b.java
    pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/graphic/StandardColorSpaceHelper.java
    pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/parser/PreflightParser.java
    pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/parser/XmlResultParser.java
    pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/process/PageTreeValidationProcess.java
    pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/Benchmark.java
    pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/TestInvalidDirectory.java
    pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/TestIsartorBavaria.java
    pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/TestPDFBox3741.java
    pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/TestValidDirectory.java
    pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/action/pdfa1b/AbstractTestAction.java
    pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/integration/AbstractInvalidFileTester.java
    pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/integration/TestValidFiles.java
    pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/metadata/TestMetadataFiles.java

Modified: pdfbox/branches/issue4569/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/issue4569/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java?rev=1872081&r1=1872080&r2=1872081&view=diff
==============================================================================
--- pdfbox/branches/issue4569/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java (original)
+++ pdfbox/branches/issue4569/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java Sun Dec 29 14:04:16 2019
@@ -760,7 +760,15 @@ public class COSParser extends BaseParse
         }
     }
 
-    private COSBase parseObjectStreamObject(int objstmObjNr, COSObjectKey key) throws IOException
+    /**
+     * Parse the object with the given key from the object stream with the given number.
+     * 
+     * @param objstmObjNr the number of the offset stream
+     * @param key the key of the object to be parsed
+     * @return the parsed object
+     * @throws IOException if something went wrong when parsing the object
+     */
+    protected COSBase parseObjectStreamObject(int objstmObjNr, COSObjectKey key) throws IOException
     {
         final COSBase objstmBaseObj = parseObjectDynamically(objstmObjNr, 0, true);
         COSBase objectStreamObject = null;

Modified: pdfbox/branches/issue4569/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/issue4569/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java?rev=1872081&r1=1872080&r2=1872081&view=diff
==============================================================================
--- pdfbox/branches/issue4569/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java (original)
+++ pdfbox/branches/issue4569/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java Sun Dec 29 14:04:16 2019
@@ -535,7 +535,7 @@ public class PDFParser extends COSParser
                 initialParse();
             }
             exceptionOccurred = false;
-            PDDocument pdDocument = new PDDocument(getDocument(), source, getAccessPermission());
+            PDDocument pdDocument = createDocument();
             pdDocument.setEncryptionDictionary(getEncryption());
             return pdDocument;
         }
@@ -549,4 +549,14 @@ public class PDFParser extends COSParser
         }
     }
 
+    /**
+     * Create the resulting document. Maybe overwritten if the parser uses another class as document.
+     * 
+     * @return the resulting document
+     * @throws IOException if the method is called before parsing the document
+     */
+    protected PDDocument createDocument() throws IOException
+    {
+        return new PDDocument(getDocument(), source, getAccessPermission());
+    }
 }

Modified: pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/PreflightDocument.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/PreflightDocument.java?rev=1872081&r1=1872080&r2=1872081&view=diff
==============================================================================
--- pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/PreflightDocument.java (original)
+++ pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/PreflightDocument.java Sun Dec 29 14:04:16 2019
@@ -22,6 +22,8 @@
 package org.apache.pdfbox.preflight;
 
 import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
 
 import org.apache.pdfbox.cos.COSDocument;
 import org.apache.pdfbox.pdmodel.PDDocument;
@@ -31,7 +33,6 @@ import org.apache.pdfbox.preflight.utils
 
 public class PreflightDocument extends PDDocument
 {
-
     private ValidationResult result = new ValidationResult(true);
 
     private PreflightConfiguration config;
@@ -43,8 +44,8 @@ public class PreflightDocument extends P
     /**
      * Create a preflight document based on the COSDocument and load the default configuration for the given format.
      * 
-     * @param doc
-     * @param format
+     * @param doc the underlying COSDocument
+     * @param format the format used for validation
      */
     public PreflightDocument(COSDocument doc, Format format)
     {
@@ -55,70 +56,69 @@ public class PreflightDocument extends P
      * Create a preflight document based on the COSDocument that will use the given configuration bean to process the
      * validation. if the configuration is null, a default configuration will be load using the given format.
      * 
-     * @param doc
-     * @param format
-     * @param config
+     * @param doc the underlying COSDocument
+     * @param format the format used for validation
+     * @param config the configuration used for validation
      */
     public PreflightDocument(COSDocument doc, Format format, PreflightConfiguration config)
     {
         super(doc);
         this.specification = format;
-        this.config = config;
-        if (this.config == null)
-        {
-            initConfiguration(format);
-        }
-    }
-
-    private void initConfiguration(Format format)
-    {
-        switch (format)
-        {
-        default: // default is PDF/A1-b
-            this.config = PreflightConfiguration.createPdfA1BConfiguration();
-            break;
-        }
-
+        // PDF/A1-b is default
+        this.config = config == null ? PreflightConfiguration.createPdfA1BConfiguration() : config;
     }
 
-    public ValidationResult getResult()
+    /**
+     * Returns an unmodifiable list of all validation errors.
+     * 
+     * @return an unmodifiable list of all validation errors
+     */
+    public List<ValidationError> getValidationErrors()
     {
-        return result;
+        return Collections.unmodifiableList(result.getErrorsList());
     }
 
-    public void setResult(ValidationResult result)
+    /**
+     * Add a validation error.
+     * 
+     * @param error the validation error to be added
+     */
+    public void addValidationError(ValidationError error)
     {
-        if (this.result != null)
-        {
-            this.result.mergeResult(result);
-        }
-        else if (result != null)
-        {
-            this.result = result;
-        }
-        else
+        if (error != null)
         {
-            this.result = new ValidationResult(true);
+            this.result.addError(error);
         }
     }
 
-    public void addValidationError(ValidationError error)
+    /**
+     * Add a list of validation errors.
+     * 
+     * @param errorList the list of validation errors
+     */
+    public void addValidationErrors(List<ValidationError> errorList)
     {
-        if (error != null)
+        if (errorList != null)
         {
-            if (result == null)
-            {
-                this.result = new ValidationResult(error.isWarning());
-            }
-            this.result.addError(error);
+            this.result.addErrors(errorList);
         }
     }
 
+    /**
+     * Returns the associated preflight context. It is created after parsing the pdf.
+     * 
+     * @return the associated preflight context
+     */
     public PreflightContext getContext()
     {
         return this.context;
     }
 
+    /**
+     * Set the preflight context for this document.
+     * 
+     * @param context the associated preflight context
+     */
     public void setContext(PreflightContext context)
     {
         this.context = context;
@@ -127,9 +127,10 @@ public class PreflightDocument extends P
     /**
      * Check that PDDocument is a valid file according to the format given during the object creation.
      * 
+     * @return the validation result
      * @throws ValidationException
      */
-    public void validate() throws ValidationException
+    public ValidationResult validate() throws ValidationException
     {
         // force early class loading to check if people forgot to use --add-modules javax.xml.bind
         // on java 9 & 10, or to add jaxb-api on java 11 and later
@@ -140,8 +141,14 @@ public class PreflightDocument extends P
         {
             ContextHelper.validateElement(context, name);
         }
+        return result;
     }
 
+    /**
+     * Returns the format which is used to valide the pdf document.
+     * 
+     * @return the format used for validation
+     */
     public Format getSpecification()
     {
         return specification;

Modified: pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/Validator_A1b.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/Validator_A1b.java?rev=1872081&r1=1872080&r2=1872081&view=diff
==============================================================================
--- pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/Validator_A1b.java (original)
+++ pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/Validator_A1b.java Sun Dec 29 14:04:16 2019
@@ -39,7 +39,6 @@ import javax.xml.transform.dom.DOMSource
 import javax.xml.transform.stream.StreamResult;
 
 import org.apache.pdfbox.preflight.ValidationResult.ValidationError;
-import org.apache.pdfbox.preflight.exception.SyntaxValidationException;
 import org.apache.pdfbox.preflight.parser.PreflightParser;
 import org.apache.pdfbox.preflight.parser.XmlResultParser;
 import org.apache.pdfbox.util.Version;
@@ -166,21 +165,7 @@ public class Validator_A1b
 
     private static int runSimple(File file) throws IOException
     {
-        ValidationResult result;
-        PreflightParser parser = new PreflightParser(file);
-        try
-        {
-            parser.parse();
-            try (PreflightDocument document = parser.getPreflightDocument())
-            {
-                document.validate();
-                result = document.getResult();
-            }
-        }
-        catch (SyntaxValidationException e)
-        {
-            result = e.getResult();
-        }
+        ValidationResult result = PreflightParser.validate(file);
 
         if (result.isValid())
         {

Modified: pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/graphic/StandardColorSpaceHelper.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/graphic/StandardColorSpaceHelper.java?rev=1872081&r1=1872080&r2=1872081&view=diff
==============================================================================
--- pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/graphic/StandardColorSpaceHelper.java (original)
+++ pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/graphic/StandardColorSpaceHelper.java Sun Dec 29 14:04:16 2019
@@ -468,9 +468,9 @@ public class StandardColorSpaceHelper im
             if (defaultCS != null)
             {
                 // defaultCS is valid if the number of errors hasn't changed
-                int nbOfErrors = context.getDocument().getResult().getErrorsList().size();
+                int nbOfErrors = context.getDocument().getValidationErrors().size();
                 processAllColorSpace(defaultCS);
-                int newNbOfErrors = context.getDocument().getResult().getErrorsList().size();
+                int newNbOfErrors = context.getDocument().getValidationErrors().size();
                 result = (nbOfErrors == newNbOfErrors);
             }
 

Modified: pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/parser/PreflightParser.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/parser/PreflightParser.java?rev=1872081&r1=1872080&r2=1872081&view=diff
==============================================================================
--- pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/parser/PreflightParser.java (original)
+++ pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/parser/PreflightParser.java Sun Dec 29 14:04:16 2019
@@ -26,26 +26,22 @@ import java.io.IOException;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
+import java.util.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import org.apache.pdfbox.cos.COSArray;
 import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSDictionary;
-import org.apache.pdfbox.cos.COSDocument;
 import org.apache.pdfbox.cos.COSFloat;
 import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.cos.COSNull;
 import org.apache.pdfbox.cos.COSNumber;
 import org.apache.pdfbox.cos.COSObject;
 import org.apache.pdfbox.cos.COSObjectKey;
 import org.apache.pdfbox.cos.COSStream;
 import org.apache.pdfbox.cos.COSString;
-import org.apache.pdfbox.io.IOUtils;
 import org.apache.pdfbox.io.RandomAccessBufferedFileInputStream;
 import org.apache.pdfbox.io.ScratchFile;
-import org.apache.pdfbox.pdfparser.PDFObjectStreamParser;
 import org.apache.pdfbox.pdfparser.PDFParser;
 import org.apache.pdfbox.pdfparser.XrefTrailerResolver.XRefType;
 import org.apache.pdfbox.pdmodel.PDDocument;
@@ -86,13 +82,15 @@ public class PreflightParser extends PDF
      * Define a one byte encoding that hasn't specific encoding in UTF-8 charset. Avoid unexpected
      * error when the encoding is Cp5816
      */
-    public static final Charset ENCODING = StandardCharsets.ISO_8859_1;
+    private static final Charset ENCODING = StandardCharsets.ISO_8859_1;
 
-    protected ValidationResult validationResult;
+    private Format format = null;
 
-    protected PreflightDocument preflightDocument;
+    private PreflightConfiguration config = null;
 
-    protected PreflightContext ctx;
+    private PreflightDocument preflightDocument;
+
+    private ValidationResult validationResult;
 
     /**
      * Constructor.
@@ -141,34 +139,13 @@ public class PreflightParser extends PDF
     }
 
     /**
-     * Create an instance of ValidationResult with a ValidationError(UNKNOWN_ERROR)
-     * 
-     * @return the ValidationError instance.
-     */
-    protected static ValidationResult createUnknownErrorResult()
-    {
-        ValidationError error = new ValidationError(PreflightConstants.ERROR_UNKNOWN_ERROR);
-        return new ValidationResult(error);
-    }
-
-    /**
-     * Add the error to the ValidationResult. If the validationResult is null, an instance is created using the
-     * isWarning boolean of the ValidationError to know if the ValidationResult must be flagged as Valid.
+     * Add a validation error to the ValidationResult.
      * 
-     * @param error
+     * @param error the validation error to be added
      */
-    protected void addValidationError(ValidationError error)
-    {
-        if (this.validationResult == null)
-        {
-            this.validationResult = new ValidationResult(error.isWarning());
-        }
-        this.validationResult.addError(error);
-    }
-
-    protected void addValidationErrors(List<ValidationError> errors)
+    private void addValidationError(ValidationError error)
     {
-        errors.forEach(this::addValidationError);
+        validationResult.addError(error);
     }
 
     @Override
@@ -201,6 +178,9 @@ public class PreflightParser extends PDF
      */
     public PDDocument parse(Format format, PreflightConfiguration config) throws IOException
     {
+        this.format = format == null ? Format.PDF_A1B : format;
+        this.config = config;
+        validationResult = new ValidationResult(true);
         PDDocument pdDocument = null;
         checkPdfHeader();
         try
@@ -211,40 +191,22 @@ public class PreflightParser extends PDF
         catch (IOException e)
         {
             addValidationError(new ValidationError(PreflightConstants.ERROR_SYNTAX_COMMON, e.getMessage()));
-            throw new SyntaxValidationException(e, this.validationResult);
+            throw new SyntaxValidationException(e, validationResult);
         }
-        finally
-        {
-            // TODO move file handling outside of the parser
-            IOUtils.closeQuietly(source);
-        }
-        Format formatToUse = (format == null ? Format.PDF_A1B : format);
-        createPdfADocument(formatToUse, config);
-        createContext();
+        // create context
+        PreflightContext context = new PreflightContext();
+        context.setDocument(preflightDocument);
+        preflightDocument.setContext(context);
+        context.setXrefTrailerResolver(xrefTrailerResolver);
+        context.setFileLen(fileLen);
+        preflightDocument.addValidationErrors(validationResult.getErrorsList());
         return pdDocument;
     }
 
-    protected void createPdfADocument(Format format, PreflightConfiguration config) throws IOException
-    {
-        COSDocument cosDocument = getDocument();
-        this.preflightDocument = new PreflightDocument(cosDocument, format, config);
-    }
-
-    /**
-     * Create a validation context. This context is set to the PreflightDocument.
-     */
-    protected void createContext()
-    {
-        ctx = new PreflightContext();
-        ctx.setDocument(preflightDocument);
-        preflightDocument.setContext(ctx);
-        ctx.setXrefTrailerResolver(xrefTrailerResolver);
-        ctx.setFileLen(this.fileLen);
-    }
-
-    public PreflightDocument getPreflightDocument() throws IOException
+    @Override
+    protected PDDocument createDocument() throws IOException
     {
-        preflightDocument.setResult(validationResult);
+        preflightDocument = new PreflightDocument(getDocument(), format, config);
         return preflightDocument;
     }
 
@@ -253,24 +215,12 @@ public class PreflightParser extends PDF
     // --------------------------------------------------------
 
     @Override
-    /**
-     * Fill the CosDocument with some object that isn't set by the NonSequentialParser
-     */
     protected void initialParse() throws IOException
     {
         super.initialParse();
-        // For each ObjectKey, we check if the object has been loaded
-        // useful for linearized PDFs
-        Map<COSObjectKey, Long> xrefTable = document.getXrefTable();
-        for (Entry<COSObjectKey, Long> entry : xrefTable.entrySet())
-        {
-            COSObject co = document.getObjectFromPool(entry.getKey());
-            if (co.getObject() == null)
-            {
-                // object isn't loaded - parse the object to load its content
-                parseObjectDynamically(co, true);
-            }
-        }
+        // Dereference each object to trigger parser validation errors
+        Set<COSObjectKey> objectKeys = document.getXrefTable().keySet();
+        objectKeys.forEach(key -> document.getObjectFromPool(key).getObject());
     }
 
     /**
@@ -278,7 +228,7 @@ public class PreflightParser extends PDF
      * be a comment with the PDF version (version 1.0 isn't conform to the PDF/A specification)
      * Second line is a comment with at least 4 bytes greater than 0x80
      */
-    protected void checkPdfHeader()
+    private void checkPdfHeader()
     {
         try
         {
@@ -292,7 +242,7 @@ public class PreflightParser extends PDF
             String secondLine = readLine();
             if (secondLine != null)
             {
-                byte[] secondLineAsBytes = secondLine.getBytes(ENCODING.name());
+                byte[] secondLineAsBytes = secondLine.getBytes(ENCODING);
                 if (secondLineAsBytes.length >= 5)
                 {
                     if (secondLineAsBytes[0] != '%')
@@ -711,11 +661,12 @@ public class PreflightParser extends PDF
         // ---- create object key and get object (container) from pool
         final COSObjectKey objKey = new COSObjectKey(objNr, objGenNr);
         final COSObject pdfObject = document.getObjectFromPool(objKey);
+        COSBase referencedObject = !pdfObject.isObjectNull() ? pdfObject.getObject() : null;
 
-        if (pdfObject.isObjectNull())
+        // not previously parsed
+        if (referencedObject == null)
         {
-            // not previously parsed
-            // ---- read offset or object stream object number from xref table
+            // read offset or object stream object number from xref table
             Long offsetOrObjstmObNr = document.getXrefTable().get(objKey);
 
             // sanity test to circumvent loops with broken documents
@@ -725,7 +676,8 @@ public class PreflightParser extends PDF
                         "Object must be defined and must not be compressed object: " + objKey.getNumber() + ":"
                                 + objKey.getGeneration()));
                 throw new SyntaxValidationException("Object must be defined and must not be compressed object: "
-                        + objKey.getNumber() + ":" + objKey.getGeneration(), validationResult);
+                                + objKey.getNumber() + ":" + objKey.getGeneration(),
+                        validationResult);
             }
 
             if (offsetOrObjstmObNr == null)
@@ -775,7 +727,8 @@ public class PreflightParser extends PDF
                             addValidationError(new ValidationError(ERROR_SYNTAX_OBJ_DELIMITER, "Expected pattern '"
                                     + new String(OBJ_MARKER) + " but missed at character '" + c + "'"));
                             throw new SyntaxValidationException("Expected pattern '" + new String(OBJ_MARKER)
-                                    + " but missed at character '" + c + "'", validationResult);
+                                            + " but missed at character '" + c + "'",
+                                    validationResult);
                         }
                     }
                 }
@@ -788,7 +741,7 @@ public class PreflightParser extends PDF
                 }
 
                 skipSpaces();
-                COSBase pb = parseDirObject();
+                referencedObject = parseDirObject();
                 skipSpaces();
                 long endObjectOffset = source.getPosition();
                 String endObjectKey = readString();
@@ -796,14 +749,14 @@ public class PreflightParser extends PDF
                 if (endObjectKey.equals("stream"))
                 {
                     source.seek(endObjectOffset);
-                    if (pb instanceof COSDictionary)
+                    if (referencedObject instanceof COSDictionary)
                     {
-                        COSStream stream = parseCOSStream((COSDictionary) pb);
+                        COSStream stream = parseCOSStream((COSDictionary) referencedObject);
                         if (securityHandler != null)
                         {
                             securityHandler.decryptStream(stream, objNr, objGenNr);
                         }
-                        pb = stream;
+                        referencedObject = stream;
                     }
                     else
                     {
@@ -828,10 +781,10 @@ public class PreflightParser extends PDF
                 }
                 else if (securityHandler != null)
                 {
-                    securityHandler.decrypt(pb, objNr, objGenNr);
+                    securityHandler.decrypt(referencedObject, objNr, objGenNr);
                 }
 
-                pdfObject.setObject(pb);
+                pdfObject.setObject(referencedObject);
 
                 if (!endObjectKey.startsWith("endobj"))
                 {
@@ -858,29 +811,20 @@ public class PreflightParser extends PDF
             }
             else
             {
-                // xref value is object nr of object stream containing object to be parsed;
+                // xref value is object nr of object stream containing object to be parsed
                 // since our object was not found it means object stream was not parsed so far
-                final int objstmObjNr = (int) (-offsetOrObjstmObNr);
-                final COSBase objstmBaseObj = parseObjectDynamically(objstmObjNr, 0, true);
-                if (objstmBaseObj instanceof COSStream)
-                {
-                    // parse object stream
-                    PDFObjectStreamParser parser = new PDFObjectStreamParser((COSStream) objstmBaseObj, document);
-                    // register all objects which are referenced to be contained in object stream
-                    parser.parse().forEach(next ->
-                    {
-                        COSObjectKey stmObjKey = new COSObjectKey(next);
-                        Long offset = document.getXrefTable().get(stmObjKey);
-                        if (offset != null && offset == -objstmObjNr)
-                        {
-                            COSObject stmObj = document.getObjectFromPool(stmObjKey);
-                            stmObj.setObject(next.getObject());
-                        }
-                    });
+                referencedObject = parseObjectStreamObject((int) -offsetOrObjstmObNr, objKey);
+                if (referencedObject != null && referencedObject != COSNull.NULL)
+                {
+                    pdfObject.setObject(referencedObject);
+                }
+                else
+                {
+                    pdfObject.setToNull();
                 }
             }
         }
-        return pdfObject.getObject();
+        return referencedObject;
     }
 
     @Override
@@ -915,4 +859,26 @@ public class PreflightParser extends PDF
         }
         return offset;
     }
+
+    /**
+     * Load and validate the given file. Returns the validation result and closes the read pdf.
+     * 
+     * @param file thew file to be read and validated
+     * @return the validation result
+     * @throws IOException in case of a file reading or parsing error
+     */
+    public static ValidationResult validate(File file) throws IOException
+    {
+        ValidationResult result;
+        PreflightParser parser = new PreflightParser(file);
+        try (PreflightDocument document = (PreflightDocument) parser.parse())
+        {
+            result = document.validate();
+        }
+        catch (SyntaxValidationException e)
+        {
+            result = e.getResult();
+        }
+        return result;
+    }
 }

Modified: pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/parser/XmlResultParser.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/parser/XmlResultParser.java?rev=1872081&r1=1872080&r2=1872081&view=diff
==============================================================================
--- pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/parser/XmlResultParser.java (original)
+++ pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/parser/XmlResultParser.java Sun Dec 29 14:04:16 2019
@@ -32,22 +32,35 @@ import java.util.Map;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 
-import org.apache.pdfbox.preflight.PreflightDocument;
+import org.apache.pdfbox.preflight.Format;
 import org.apache.pdfbox.preflight.ValidationResult;
 import org.apache.pdfbox.preflight.ValidationResult.ValidationError;
-import org.apache.pdfbox.preflight.exception.SyntaxValidationException;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 
 public class XmlResultParser
 {
 
-
+    /**
+     * Validate the given file.
+     * 
+     * @param file the file to be validated
+     * @return the validation results as XML
+     * @throws IOException if something went wrong
+     */
     public Element validate(File file) throws IOException
     {
         return validate(file, file.getName());
     }
 
+    /**
+     * Validate the given file. Add the validation results to the given XML tree.
+     * 
+     * @param rdocument XML based validation results of a former run
+     * @param file the file to be validated
+     * @return the validation results as XML
+     * @throws IOException if something went wrong
+     */
     public Element validate(Document rdocument, File file) throws IOException
     {
         return validate(rdocument, file, file.getName());
@@ -71,26 +84,12 @@ public class XmlResultParser
     private Element validate(Document rdocument, File file, String name)
             throws IOException
     {
-        String pdfType = null;
+        String pdfType = Format.PDF_A1B.getFname();
         ValidationResult result;
         long before = System.currentTimeMillis();
         try
         {
-            PreflightParser parser = new PreflightParser(file);
-            try
-            {
-                parser.parse();
-                try (PreflightDocument document = parser.getPreflightDocument())
-                {
-                    document.validate();
-                    pdfType = document.getSpecification().getFname();
-                    result = document.getResult();
-                }
-            }
-            catch (SyntaxValidationException e)
-            {
-                result = e.getResult();
-            }
+            result = PreflightParser.validate(file);
         } 
         catch(Exception e) 
         {
@@ -173,7 +172,8 @@ public class XmlResultParser
          return cleaned;
     }
 
-    protected Element generateFailureResponse(Document rdocument, String name,long duration, String pdfType, Exception e)
+    private Element generateFailureResponse(Document rdocument, String name, long duration,
+            String pdfType, Exception e)
     {
         Element preflight = generateResponseSkeleton(rdocument, name, duration);
         // valid ?

Modified: pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/process/PageTreeValidationProcess.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/process/PageTreeValidationProcess.java?rev=1872081&r1=1872080&r2=1872081&view=diff
==============================================================================
--- pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/process/PageTreeValidationProcess.java (original)
+++ pdfbox/branches/issue4569/preflight/src/main/java/org/apache/pdfbox/preflight/process/PageTreeValidationProcess.java Sun Dec 29 14:04:16 2019
@@ -57,7 +57,8 @@ public class PageTreeValidationProcess e
                 context.setCurrentPageNumber(p);
                 validatePage(context, page);
 
-                if (context.getDocument().getResult().getErrorsList().size() > context.getConfig().getMaxErrors())
+                if (context.getDocument().getValidationErrors().size() > context.getConfig()
+                        .getMaxErrors())
                 {
                     context.addValidationError(new ValidationError(PreflightConstants.ERROR_UNKNOWN_ERROR, 
                             "Over " + context.getConfig().getMaxErrors() +
@@ -74,7 +75,7 @@ public class PageTreeValidationProcess e
         }
     }
 
-    protected void validatePage(PreflightContext context, PDPage page) throws ValidationException
+    private void validatePage(PreflightContext context, PDPage page) throws ValidationException
     {
         ContextHelper.validateElement(context, page, PAGE_PROCESS);
     }

Modified: pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/Benchmark.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/Benchmark.java?rev=1872081&r1=1872080&r2=1872081&view=diff
==============================================================================
--- pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/Benchmark.java (original)
+++ pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/Benchmark.java Sun Dec 29 14:04:16 2019
@@ -75,19 +75,13 @@ public class Benchmark
         {
             File file = lfd.get(i % size);
             long startLTime = System.currentTimeMillis();
-            PreflightParser parser = new PreflightParser(file);
-            parser.parse();
-            try (PreflightDocument document = parser.getPreflightDocument())
+            ValidationResult result = PreflightParser.validate(file);
+            if (!result.isValid())
             {
-                document.validate();
-                ValidationResult result = document.getResult();
-                if (!result.isValid())
+                resFile.write(file.getAbsolutePath() + " isn't PDF/A\n");
+                for (ValidationError error : result.getErrorsList())
                 {
-                    resFile.write(file.getAbsolutePath() + " isn't PDF/A\n");
-                    for (ValidationError error : result.getErrorsList())
-                    {
-                        resFile.write(error.getErrorCode() + " : " + error.getDetails() + "\n");
-                    }
+                    resFile.write(error.getErrorCode() + " : " + error.getDetails() + "\n");
                 }
             }
             long endLTime = System.currentTimeMillis();

Modified: pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/TestInvalidDirectory.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/TestInvalidDirectory.java?rev=1872081&r1=1872080&r2=1872081&view=diff
==============================================================================
--- pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/TestInvalidDirectory.java (original)
+++ pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/TestInvalidDirectory.java Sun Dec 29 14:04:16 2019
@@ -26,7 +26,6 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 
-import org.apache.pdfbox.preflight.exception.SyntaxValidationException;
 import org.apache.pdfbox.preflight.parser.PreflightParser;
 import org.junit.Assert;
 import org.junit.Test;
@@ -50,29 +49,8 @@ public class TestInvalidDirectory
     @Test
     public void validate() throws Exception
     {
-        PreflightDocument document = null;
-
         System.out.println(target);
-        ValidationResult result = null;
-        try
-        {
-            PreflightParser parser = new PreflightParser(target);
-            parser.parse();
-            document = parser.getPreflightDocument();
-            document.validate();
-            result = document.getResult();
-        }
-        catch (SyntaxValidationException e)
-        {
-            result = e.getResult();
-        }
-        finally
-        {
-            if (document != null)
-            {
-                document.close();
-            }
-        }
+        ValidationResult result = PreflightParser.validate(target);
         Assert.assertFalse("Test of " + target, result.isValid());
 
     }

Modified: pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/TestIsartorBavaria.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/TestIsartorBavaria.java?rev=1872081&r1=1872080&r2=1872081&view=diff
==============================================================================
--- pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/TestIsartorBavaria.java (original)
+++ pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/TestIsartorBavaria.java Sun Dec 29 14:04:16 2019
@@ -36,7 +36,6 @@ import java.util.StringTokenizer;
 import org.apache.commons.io.FileUtils;
 import org.apache.pdfbox.io.IOUtils;
 import org.apache.pdfbox.preflight.ValidationResult.ValidationError;
-import org.apache.pdfbox.preflight.exception.SyntaxValidationException;
 import org.apache.pdfbox.preflight.parser.PreflightParser;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
@@ -175,27 +174,9 @@ public class TestIsartorBavaria
     @Test()
     public void validate() throws Exception
     {
-        PreflightDocument document = null;
-        try
+        ValidationResult result = PreflightParser.validate(file);
+        if (result != null)
         {
-            ValidationResult result;
-            try
-            {
-                PreflightParser parser = new PreflightParser(file);
-                parser.parse();
-                document = (PreflightDocument) parser.getPreflightDocument();
-                // to speeds up tests, skip validation of page count is over the limit
-                if (document.getNumberOfPages() < 8191)
-                {
-                    document.validate();
-                }
-                result = document.getResult();
-            }
-            catch (SyntaxValidationException e)
-            {
-                result = e.getResult();
-            }
-            
             if (this.expectedErrorSet.isEmpty())
             {
                 Set<String> errorSet = new HashSet<>();
@@ -205,7 +186,7 @@ public class TestIsartorBavaria
                 }
                 StringBuilder message = new StringBuilder();
                 message.append(file.getName());
-                message.append( " : PDF/A file should be valid, but has error");
+                message.append(" : PDF/A file should be valid, but has error");
                 if (errorSet.size() > 1)
                 {
                     message.append('s');
@@ -221,8 +202,8 @@ public class TestIsartorBavaria
             }
             else
             {
-                assertFalse(file.getName() + " : PDF/A file should be invalid (expected " +
-                        this.expectedErrorSet + ")", result.isValid()); //TODO
+                assertFalse(file.getName() + " : PDF/A file should be invalid (expected "
+                        + this.expectedErrorSet + ")", result.isValid()); // TODO
 
                 assertTrue(file.getName() + " : Should find at least one error",
                         result.getErrorsList().size() > 0);
@@ -241,8 +222,8 @@ public class TestIsartorBavaria
                         }
                         if (isartorResultFile != null && !logged)
                         {
-                            String log = file.getName().replace(".pdf", "") + "#" + error.getErrorCode() +
-                                    "#" + error.getDetails() + "\n";
+                            String log = file.getName().replace(".pdf", "") + "#"
+                                    + error.getErrorCode() + "#" + error.getDetails() + "\n";
                             isartorResultFile.write(log.getBytes());
                         }
                     }
@@ -251,7 +232,7 @@ public class TestIsartorBavaria
                         allFound = false;
                         break;
                     }
-                    
+
                     // log only in the first inner loop
                     logged = true;
                 }
@@ -277,12 +258,5 @@ public class TestIsartorBavaria
                 // if each of the expected errors are found, we consider test valid
             }
         }
-        finally
-        {
-            if (document != null)
-            {
-                document.close();
-            }
-        }
     }
 }

Modified: pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/TestPDFBox3741.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/TestPDFBox3741.java?rev=1872081&r1=1872080&r2=1872081&view=diff
==============================================================================
--- pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/TestPDFBox3741.java (original)
+++ pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/TestPDFBox3741.java Sun Dec 29 14:04:16 2019
@@ -37,16 +37,8 @@ public class TestPDFBox3741
     @Test
     public void testPDFBox3741() throws IOException
     {
-        PreflightParser parser = new PreflightParser(
-                new File("src/test/resources/PDFBOX-3741.pdf"));
-        parser.parse();
-        ValidationResult result;
-        try (PreflightDocument document = parser.getPreflightDocument())
-        {
-            document.validate();
-            result = document.getResult();
-        }
-
+        ValidationResult result = PreflightParser
+                .validate(new File("src/test/resources/PDFBOX-3741.pdf"));
         // Error should be:
         // 2.4.3: Invalid Color space, /DeviceGray default for operator "Tj" can't be used without Color Profile
         Assert.assertFalse("File PDFBOX-3741.pdf should be detected as not PDF/A-1b", result.isValid());

Modified: pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/TestValidDirectory.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/TestValidDirectory.java?rev=1872081&r1=1872080&r2=1872081&view=diff
==============================================================================
--- pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/TestValidDirectory.java (original)
+++ pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/TestValidDirectory.java Sun Dec 29 14:04:16 2019
@@ -26,7 +26,6 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 
-import org.apache.pdfbox.preflight.exception.SyntaxValidationException;
 import org.apache.pdfbox.preflight.parser.PreflightParser;
 import org.junit.Assert;
 import org.junit.Test;
@@ -48,28 +47,8 @@ public class TestValidDirectory
     @Test
     public void validate() throws Exception
     {
-        PreflightDocument document = null;
         System.out.println(target);
-        ValidationResult result = null;
-        try
-        {
-            PreflightParser parser = new PreflightParser(target);
-            parser.parse();
-            document = (PreflightDocument) parser.getPreflightDocument();
-            document.validate();
-            result = document.getResult();
-        }
-        catch (SyntaxValidationException e)
-        {
-            result = e.getResult();
-        }
-        finally
-        {
-            if (document != null)
-            {
-                document.close();
-            }
-        }
+        ValidationResult result = PreflightParser.validate(target);
         Assert.assertTrue("Validation of " + target, result.isValid());
     }
 

Modified: pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/action/pdfa1b/AbstractTestAction.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/action/pdfa1b/AbstractTestAction.java?rev=1872081&r1=1872080&r2=1872081&view=diff
==============================================================================
--- pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/action/pdfa1b/AbstractTestAction.java (original)
+++ pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/action/pdfa1b/AbstractTestAction.java Sun Dec 29 14:04:16 2019
@@ -108,12 +108,12 @@ public abstract class AbstractTestAction
             abstractActionManager.valid();
         }
 
+        List<ValidationError> errors = ctx.getDocument().getValidationErrors();
         // check the result
         if (!valid)
         {
-            List<ValidationError> errors = ctx.getDocument().getResult().getErrorsList();
             assertFalse(errors.isEmpty());
-            if (expectedCode != null || !"".equals(expectedCode))
+            if (expectedCode != null && !expectedCode.isEmpty())
             {
                 boolean found = false;
                 for (ValidationError err : errors)
@@ -129,11 +129,7 @@ public abstract class AbstractTestAction
         }
         else
         {
-            if (ctx.getDocument().getResult() != null)
-            {
-                List<ValidationError> errors = ctx.getDocument().getResult().getErrorsList();
-                assertTrue(errors.isEmpty());
-            }
+            assertTrue(errors.isEmpty());
         }
         ctx.getDocument().close();
     }

Modified: pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/integration/AbstractInvalidFileTester.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/integration/AbstractInvalidFileTester.java?rev=1872081&r1=1872080&r2=1872081&view=diff
==============================================================================
--- pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/integration/AbstractInvalidFileTester.java (original)
+++ pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/integration/AbstractInvalidFileTester.java Sun Dec 29 14:04:16 2019
@@ -28,10 +28,8 @@ import java.io.OutputStream;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.pdfbox.io.IOUtils;
-import org.apache.pdfbox.preflight.PreflightDocument;
 import org.apache.pdfbox.preflight.ValidationResult;
 import org.apache.pdfbox.preflight.ValidationResult.ValidationError;
-import org.apache.pdfbox.preflight.exception.ValidationException;
 import org.apache.pdfbox.preflight.parser.PreflightParser;
 import org.junit.After;
 import org.junit.Assert;
@@ -86,74 +84,57 @@ public abstract class AbstractInvalidFil
             logger.warn("This is an empty test");
             return;
         }
-        PreflightDocument document = null;
-        try
+        ValidationResult result = PreflightParser.validate(path);
+        Assert.assertFalse(path + " : Isartor file should be invalid (" + path + ")",
+                result.isValid());
+        Assert.assertTrue(path + " : Should find at least one error",
+                result.getErrorsList().size() > 0);
+        // could contain more than one error
+        boolean found = false;
+        if (this.expectedError != null)
         {
-            PreflightParser parser = new PreflightParser(path);
-            parser.parse();
-            document = parser.getPreflightDocument();
-            document.validate();
-
-            ValidationResult result = document.getResult();
-            Assert.assertFalse(path + " : Isartor file should be invalid (" + path + ")", result.isValid());
-            Assert.assertTrue(path + " : Should find at least one error", result.getErrorsList().size() > 0);
-            // could contain more than one error
-            boolean found = false;
-            if (this.expectedError != null)
+            for (ValidationError error : result.getErrorsList())
             {
-                for (ValidationError error : result.getErrorsList())
+                if (error.getErrorCode().equals(this.expectedError))
                 {
-                    if (error.getErrorCode().equals(this.expectedError))
-                    {
-                        found = true;
-                        if (outputResult == null)
-                        {
-                            break;
-                        }
-                    }
-                    if (outputResult != null)
+                    found = true;
+                    if (outputResult == null)
                     {
-                        String log = path.getName().replace(".pdf", "") + "#" + error.getErrorCode() + "#"
-                                + error.getDetails() + "\n";
-                        outputResult.write(log.getBytes());
+                        break;
                     }
                 }
+                if (outputResult != null)
+                {
+                    String log = path.getName().replace(".pdf", "") + "#" + error.getErrorCode()
+                            + "#" + error.getDetails() + "\n";
+                    outputResult.write(log.getBytes());
+                }
             }
+        }
 
-            if (result.getErrorsList().size() > 0)
+        if (result.getErrorsList().size() > 0)
+        {
+            if (this.expectedError == null)
             {
-                if (this.expectedError == null)
-                {
-                    logger.info("File invalid as expected (no expected code) :" + this.path.getAbsolutePath());
-                }
-                else if (!found)
-                {
-                    StringBuilder message = new StringBuilder(100);
-                    message.append(path).append(" : Invalid error code returned. Expected ");
-                    message.append(this.expectedError).append(", found ");
-                    for (ValidationError error : result.getErrorsList())
-                    {
-                        message.append(error.getErrorCode()).append(" ");
-                    }
-                    Assert.fail(message.toString());
-                }
+                logger.info("File invalid as expected (no expected code) :"
+                        + this.path.getAbsolutePath());
             }
-            else
+            else if (!found)
             {
-                Assert.assertEquals(path + " : Invalid error code returned.", this.expectedError, result
-                        .getErrorsList().get(0).getErrorCode());
+                StringBuilder message = new StringBuilder(100);
+                message.append(path).append(" : Invalid error code returned. Expected ");
+                message.append(this.expectedError).append(", found ");
+                for (ValidationError error : result.getErrorsList())
+                {
+                    message.append(error.getErrorCode()).append(" ");
+                }
+                Assert.fail(message.toString());
             }
         }
-        catch (ValidationException e)
-        {
-            throw new Exception(path + " :" + e.getMessage(), e);
-        }
-        finally
+        else
         {
-            if (document != null)
-            {
-                document.close();
-            }
+            Assert.assertEquals(path + " : Invalid error code returned.", this.expectedError,
+                    result.getErrorsList().get(0).getErrorCode());
         }
     }
 

Modified: pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/integration/TestValidFiles.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/integration/TestValidFiles.java?rev=1872081&r1=1872080&r2=1872081&view=diff
==============================================================================
--- pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/integration/TestValidFiles.java (original)
+++ pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/integration/TestValidFiles.java Sun Dec 29 14:04:16 2019
@@ -32,9 +32,7 @@ import org.apache.commons.io.FileUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.pdfbox.io.IOUtils;
-import org.apache.pdfbox.preflight.PreflightDocument;
 import org.apache.pdfbox.preflight.ValidationResult;
-import org.apache.pdfbox.preflight.exception.ValidationException;
 import org.apache.pdfbox.preflight.parser.PreflightParser;
 import org.junit.AfterClass;
 import org.junit.Assert;
@@ -129,33 +127,16 @@ public class TestValidFiles
             logger.warn("This is an empty test");
             return;
         }
-        PreflightDocument document = null;
-        try
+        ValidationResult result = PreflightParser.validate(path);
+
+        Assert.assertFalse(path + " : Isartor file should be invalid (" + path + ")",
+                result.isValid());
+        Assert.assertTrue(path + " : Should find at least one error",
+                result.getErrorsList().size() > 0);
+        // could contain more than one error
+        if (result.getErrorsList().size() > 0)
         {
-            PreflightParser parser = new PreflightParser(path);
-            parser.parse();
-            document = parser.getPreflightDocument();
-            document.validate();
-
-            ValidationResult result = document.getResult();
-            Assert.assertFalse(path + " : Isartor file should be invalid (" + path + ")", result.isValid());
-            Assert.assertTrue(path + " : Should find at least one error", result.getErrorsList().size() > 0);
-            // could contain more than one error
-            if (result.getErrorsList().size() > 0)
-            {
-                Assert.fail("File expected valid : " + path.getAbsolutePath());
-            }
-        }
-        catch (ValidationException e)
-        {
-            throw new Exception(path + " :" + e.getMessage(), e);
-        }
-        finally
-        {
-            if (document != null)
-            {
-                document.close();
-            }
+            Assert.fail("File expected valid : " + path.getAbsolutePath());
         }
     }
 

Modified: pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/metadata/TestMetadataFiles.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/metadata/TestMetadataFiles.java?rev=1872081&r1=1872080&r2=1872081&view=diff
==============================================================================
--- pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/metadata/TestMetadataFiles.java (original)
+++ pdfbox/branches/issue4569/preflight/src/test/java/org/apache/pdfbox/preflight/metadata/TestMetadataFiles.java Sun Dec 29 14:04:16 2019
@@ -28,9 +28,7 @@ import static org.junit.Assert.fail;
 import java.io.File;
 import java.io.IOException;
 
-import org.apache.pdfbox.preflight.PreflightDocument;
 import org.apache.pdfbox.preflight.ValidationResult;
-import org.apache.pdfbox.preflight.exception.SyntaxValidationException;
 import org.apache.pdfbox.preflight.parser.PreflightParser;
 import org.junit.Test;
 
@@ -67,22 +65,13 @@ public class TestMetadataFiles
 
     private boolean checkPDF(File pdf)
     {
-        PreflightDocument document = null;
         boolean testResult = false;
         if (pdf.exists())
         {
             ValidationResult result = null;
             try
             {
-                PreflightParser parser = new PreflightParser(pdf);
-                parser.parse();
-                document = parser.getPreflightDocument();
-                document.validate();
-                result = document.getResult();
-            }
-            catch (SyntaxValidationException e)
-            {
-                result = e.getResult();
+                result = PreflightParser.validate(pdf);
             }
             catch (IOException e)
             {
@@ -92,17 +81,6 @@ public class TestMetadataFiles
             {
                 testResult = result.isValid();
             }
-            if (document != null)
-            {
-                try
-                {
-                    document.close();
-                }
-                catch (IOException e)
-                {
-                    // shouldn't happen;
-                }
-            }
         }
         else
         {