Search in sources :

Example 6 with PreflightParser

use of org.apache.pdfbox.preflight.parser.PreflightParser in project pdfbox by apache.

the class TestValidFiles method validate.

@Test()
public void validate() throws Exception {
    if (path == null) {
        logger.warn("This is an empty test");
        return;
    }
    PreflightDocument document = null;
    try {
        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();
        }
    }
}
Also used : ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) ValidationResult(org.apache.pdfbox.preflight.ValidationResult) PreflightParser(org.apache.pdfbox.preflight.parser.PreflightParser) PreflightDocument(org.apache.pdfbox.preflight.PreflightDocument) ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) Test(org.junit.Test)

Example 7 with PreflightParser

use of org.apache.pdfbox.preflight.parser.PreflightParser in project pdfbox by apache.

the class TestMetadataFiles method checkPDF.

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 = (PreflightDocument) parser.getPDDocument();
            document.validate();
            result = document.getResult();
        } catch (SyntaxValidationException e) {
            result = e.getResult();
        } catch (IOException e) {
            fail("An exception occured while parsing the PDF " + pdf + ": " + e);
        }
        if (result != null) {
            testResult = result.isValid();
        }
        if (document != null) {
            try {
                document.close();
            } catch (IOException e) {
            // shouldn't happen;
            }
        }
    } else {
        fail("Can't find the given file " + pdf);
    }
    return testResult;
}
Also used : SyntaxValidationException(org.apache.pdfbox.preflight.exception.SyntaxValidationException) IOException(java.io.IOException) ValidationResult(org.apache.pdfbox.preflight.ValidationResult) PreflightParser(org.apache.pdfbox.preflight.parser.PreflightParser) PreflightDocument(org.apache.pdfbox.preflight.PreflightDocument)

Example 8 with PreflightParser

use of org.apache.pdfbox.preflight.parser.PreflightParser in project pdfbox by apache.

the class TestValidDirectory method validate.

@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.getPDDocument();
        document.validate();
        result = document.getResult();
    } catch (SyntaxValidationException e) {
        result = e.getResult();
    } finally {
        if (document != null) {
            document.close();
        }
    }
    Assert.assertTrue("Validation of " + target, result.isValid());
}
Also used : SyntaxValidationException(org.apache.pdfbox.preflight.exception.SyntaxValidationException) PreflightParser(org.apache.pdfbox.preflight.parser.PreflightParser) Test(org.junit.Test)

Example 9 with PreflightParser

use of org.apache.pdfbox.preflight.parser.PreflightParser in project pdfbox by apache.

the class TestInvalidDirectory method validate.

@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.getPDDocument();
        document.validate();
        result = document.getResult();
    } catch (SyntaxValidationException e) {
        result = e.getResult();
    } finally {
        if (document != null) {
            document.close();
        }
    }
    Assert.assertFalse("Test of " + target, result.isValid());
}
Also used : SyntaxValidationException(org.apache.pdfbox.preflight.exception.SyntaxValidationException) PreflightParser(org.apache.pdfbox.preflight.parser.PreflightParser) Test(org.junit.Test)

Example 10 with PreflightParser

use of org.apache.pdfbox.preflight.parser.PreflightParser in project pdfbox by apache.

the class TestIsartorBavaria method validate.

@Test()
public void validate() throws Exception {
    PreflightDocument document = null;
    try {
        ValidationResult result;
        try {
            PreflightParser parser = new PreflightParser(file);
            parser.parse();
            document = (PreflightDocument) parser.getPDDocument();
            // 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<>();
            for (ValidationError error : result.getErrorsList()) {
                errorSet.add(error.getErrorCode());
            }
            StringBuilder message = new StringBuilder();
            message.append(file.getName());
            message.append(" : PDF/A file should be valid, but has error");
            if (errorSet.size() > 1) {
                message.append('s');
            }
            message.append(':');
            for (String errMsg : errorSet) {
                message.append(' ');
                message.append(errMsg);
            }
            assertTrue(message.toString(), result.isValid());
            assertTrue(message.toString(), result.getErrorsList().isEmpty());
        } else {
            assertFalse(file.getName() + " : PDF/A file should be invalid (expected " + this.expectedErrorSet + ")", // TODO
            result.isValid());
            assertTrue(file.getName() + " : Should find at least one error", result.getErrorsList().size() > 0);
            // each expected error should occur
            boolean logged = false;
            boolean allFound = true;
            for (String expectedError : this.expectedErrorSet) {
                boolean oneFound = false;
                for (ValidationError error : result.getErrorsList()) {
                    if (error.getErrorCode().equals(expectedError)) {
                        oneFound = true;
                    }
                    if (isartorResultFile != null && !logged) {
                        String log = file.getName().replace(".pdf", "") + "#" + error.getErrorCode() + "#" + error.getDetails() + "\n";
                        isartorResultFile.write(log.getBytes());
                    }
                }
                if (!oneFound) {
                    allFound = false;
                    break;
                }
                // log only in the first inner loop
                logged = true;
            }
            if (!allFound) {
                Set<String> errorSet = new HashSet<>();
                for (ValidationError error : result.getErrorsList()) {
                    errorSet.add(error.getErrorCode());
                }
                StringBuilder message = new StringBuilder();
                for (String errMsg : errorSet) {
                    if (message.length() > 0) {
                        message.append(", ");
                    }
                    message.append(errMsg);
                }
                fail(String.format("%s : Invalid error code returned. Expected %s, found [%s]", file.getName(), expectedErrorSet, message.toString().trim()));
            }
        // if each of the expected errors are found, we consider test valid
        }
    } finally {
        if (document != null) {
            document.close();
        }
    }
}
Also used : SyntaxValidationException(org.apache.pdfbox.preflight.exception.SyntaxValidationException) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) PreflightParser(org.apache.pdfbox.preflight.parser.PreflightParser) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

PreflightParser (org.apache.pdfbox.preflight.parser.PreflightParser)10 Test (org.junit.Test)6 ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)5 SyntaxValidationException (org.apache.pdfbox.preflight.exception.SyntaxValidationException)5 PreflightDocument (org.apache.pdfbox.preflight.PreflightDocument)4 ValidationResult (org.apache.pdfbox.preflight.ValidationResult)4 File (java.io.File)2 ValidationException (org.apache.pdfbox.preflight.exception.ValidationException)2 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 DataSource (javax.activation.DataSource)1 FileDataSource (javax.activation.FileDataSource)1 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)1 PDDocumentCatalog (org.apache.pdfbox.pdmodel.PDDocumentCatalog)1 PDMetadata (org.apache.pdfbox.pdmodel.common.PDMetadata)1 XMPMetadata (org.apache.xmpbox.XMPMetadata)1