Search in sources :

Example 6 with ValidationResult

use of org.apache.pdfbox.preflight.ValidationResult 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 ValidationResult

use of org.apache.pdfbox.preflight.ValidationResult 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 ValidationResult

use of org.apache.pdfbox.preflight.ValidationResult in project pdfbox by apache.

the class TestXmlResultParser method testSameErrorTwice.

@Test
public void testSameErrorTwice() throws Exception {
    ValidationResult result = new ValidationResult(false);
    result.addError(new ValidationResult.ValidationError(ERROR_CODE));
    result.addError(new ValidationResult.ValidationError(ERROR_CODE));
    parser.createResponseWithError(document, "pdftype", result, preflight);
    Assert.assertNotNull(xpath.evaluate("errors[@count='2']", preflight, XPathConstants.NODE));
    Assert.assertNotNull(xpath.evaluate("errors/error[@count='2']", preflight, XPathConstants.NODE));
    Element code = (Element) xpath.evaluate("errors/error[@count='2']/code", preflight, XPathConstants.NODE);
    Assert.assertNotNull(code);
    Assert.assertEquals(ERROR_CODE, code.getTextContent());
}
Also used : Element(org.w3c.dom.Element) ValidationResult(org.apache.pdfbox.preflight.ValidationResult) Test(org.junit.Test)

Example 9 with ValidationResult

use of org.apache.pdfbox.preflight.ValidationResult in project pdfbox by apache.

the class TestXmlResultParser method testSameCodeWithDifferentMessages.

@Test
public void testSameCodeWithDifferentMessages() throws Exception {
    ValidationResult result = new ValidationResult(false);
    result.addError(new ValidationResult.ValidationError(ERROR_CODE, "message 1"));
    result.addError(new ValidationResult.ValidationError(ERROR_CODE, "message 2"));
    parser.createResponseWithError(document, "pdftype", result, preflight);
    Assert.assertNotNull(xpath.evaluate("errors[@count='2']", preflight, XPathConstants.NODE));
    NodeList nl = (NodeList) xpath.evaluate("errors/error[@count='1']", preflight, XPathConstants.NODESET);
    Assert.assertEquals(2, nl.getLength());
}
Also used : NodeList(org.w3c.dom.NodeList) ValidationResult(org.apache.pdfbox.preflight.ValidationResult) Test(org.junit.Test)

Example 10 with ValidationResult

use of org.apache.pdfbox.preflight.ValidationResult in project pdfbox by apache.

the class TestXmlResultParser method testTwoError.

@Test
public void testTwoError() throws Exception {
    ValidationResult result = new ValidationResult(false);
    result.addError(new ValidationResult.ValidationError("7"));
    result.addError(new ValidationResult.ValidationError(ERROR_CODE));
    parser.createResponseWithError(document, "pdftype", result, preflight);
    Assert.assertNotNull(xpath.evaluate("errors[@count='2']", preflight, XPathConstants.NODE));
    NodeList nl = (NodeList) xpath.evaluate("errors/error[@count='1']", preflight, XPathConstants.NODESET);
    Assert.assertEquals(2, nl.getLength());
}
Also used : NodeList(org.w3c.dom.NodeList) ValidationResult(org.apache.pdfbox.preflight.ValidationResult) Test(org.junit.Test)

Aggregations

ValidationResult (org.apache.pdfbox.preflight.ValidationResult)10 PreflightDocument (org.apache.pdfbox.preflight.PreflightDocument)6 Test (org.junit.Test)6 PreflightParser (org.apache.pdfbox.preflight.parser.PreflightParser)4 ValidationException (org.apache.pdfbox.preflight.exception.ValidationException)3 NodeList (org.w3c.dom.NodeList)3 IOException (java.io.IOException)2 ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)2 SyntaxValidationException (org.apache.pdfbox.preflight.exception.SyntaxValidationException)2 Element (org.w3c.dom.Element)2 File (java.io.File)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)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 DublinCoreSchema (org.apache.xmpbox.schema.DublinCoreSchema)1 DomXmpParser (org.apache.xmpbox.xml.DomXmpParser)1