Search in sources :

Example 1 with ValidationResult

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

the class XmlResultParser method validate.

public Element validate(Document rdocument, DataSource source) throws IOException {
    String pdfType = null;
    ValidationResult result;
    long before = System.currentTimeMillis();
    try {
        PreflightParser parser = new PreflightParser(source);
        try {
            parser.parse();
            PreflightDocument document = parser.getPreflightDocument();
            document.validate();
            pdfType = document.getSpecification().getFname();
            result = document.getResult();
            document.close();
        } catch (SyntaxValidationException e) {
            result = e.getResult();
        }
    } catch (Exception e) {
        long after = System.currentTimeMillis();
        return generateFailureResponse(rdocument, source.getName(), after - before, pdfType, e);
    }
    long after = System.currentTimeMillis();
    if (result.isValid()) {
        Element preflight = generateResponseSkeleton(rdocument, source.getName(), after - before);
        // valid ?
        Element valid = rdocument.createElement("isValid");
        valid.setAttribute("type", pdfType);
        valid.setTextContent("true");
        preflight.appendChild(valid);
        return preflight;
    } else {
        Element preflight = generateResponseSkeleton(rdocument, source.getName(), after - before);
        // valid ?
        createResponseWithError(rdocument, pdfType, result, preflight);
        return preflight;
    }
}
Also used : SyntaxValidationException(org.apache.pdfbox.preflight.exception.SyntaxValidationException) Element(org.w3c.dom.Element) ValidationResult(org.apache.pdfbox.preflight.ValidationResult) PreflightDocument(org.apache.pdfbox.preflight.PreflightDocument) IOException(java.io.IOException) SyntaxValidationException(org.apache.pdfbox.preflight.exception.SyntaxValidationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 2 with ValidationResult

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

the class AbstractInvalidFileTester method validate.

@Test()
public final 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
        boolean found = false;
        if (this.expectedError != null) {
            for (ValidationError error : result.getErrorsList()) {
                if (error.getErrorCode().equals(this.expectedError)) {
                    found = true;
                    if (outputResult == null) {
                        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 (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());
            }
        } else {
            Assert.assertEquals(path + " : Invalid error code returned.", this.expectedError, result.getErrorsList().get(0).getErrorCode());
        }
    } 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) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) 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 3 with ValidationResult

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

the class TestXmlResultParser method testOneError.

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

Example 4 with ValidationResult

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

the class CreatePDFATest method testCreatePDFA.

/**
 * Test of doIt method of class CreatePDFA.
 */
public void testCreatePDFA() throws Exception {
    System.out.println("testCreatePDFA");
    String pdfaFilename = outDir + "/PDFA.pdf";
    String message = "The quick brown fox jumps over the lazy dog äöüÄÖÜß @°^²³ {[]}";
    String dir = "../pdfbox/src/main/resources/org/apache/pdfbox/resources/ttf/";
    String fontfile = dir + "LiberationSans-Regular.ttf";
    CreatePDFA.main(new String[] { pdfaFilename, message, fontfile });
    PreflightParser preflightParser = new PreflightParser(new File(pdfaFilename));
    preflightParser.parse();
    try (PreflightDocument preflightDocument = preflightParser.getPreflightDocument()) {
        preflightDocument.validate();
        ValidationResult result = preflightDocument.getResult();
        for (ValidationError ve : result.getErrorsList()) {
            System.err.println(ve.getErrorCode() + ": " + ve.getDetails());
        }
        assertTrue("PDF file created with CreatePDFA is not valid PDF/A-1b", result.isValid());
    }
    // check the XMP metadata
    PDDocument document = PDDocument.load(new File(pdfaFilename));
    PDDocumentCatalog catalog = document.getDocumentCatalog();
    PDMetadata meta = catalog.getMetadata();
    DomXmpParser xmpParser = new DomXmpParser();
    XMPMetadata metadata = xmpParser.parse(meta.createInputStream());
    DublinCoreSchema dc = metadata.getDublinCoreSchema();
    assertEquals(pdfaFilename, dc.getTitle());
    document.close();
}
Also used : XMPMetadata(org.apache.xmpbox.XMPMetadata) DomXmpParser(org.apache.xmpbox.xml.DomXmpParser) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) PDMetadata(org.apache.pdfbox.pdmodel.common.PDMetadata) ValidationResult(org.apache.pdfbox.preflight.ValidationResult) DublinCoreSchema(org.apache.xmpbox.schema.DublinCoreSchema) PreflightParser(org.apache.pdfbox.preflight.parser.PreflightParser) File(java.io.File) PreflightDocument(org.apache.pdfbox.preflight.PreflightDocument) PDDocumentCatalog(org.apache.pdfbox.pdmodel.PDDocumentCatalog)

Example 5 with ValidationResult

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

the class ZUGFeRDExporter method getA1ParserValidationResult.

private boolean getA1ParserValidationResult(PreflightParser parser) throws IOException {
    ValidationResult result = null;
    try {
        /*
			 * Parse the PDF file with PreflightParser that inherits from the
			 * NonSequentialParser. Some additional controls are present to
			 * check a set of PDF/A requirements. (Stream length consistency,
			 * EOL after some Keyword...)
			 */
        parser.parse();
        /*
			 * Once the syntax validation is done, the parser can provide a
			 * PreflightDocument (that inherits from PDDocument) This document
			 * process the end of PDF/A validation.
			 */
        PreflightDocument document = parser.getPreflightDocument();
        document.validate();
        // Get validation result
        result = document.getResult();
        document.close();
    } catch (ValidationException e) {
        /*
			 * the parse method can throw a SyntaxValidationException if the PDF
			 * file can't be parsed. In this case, the exception contains an
			 * instance of ValidationResult
			 */
        return false;
    }
    // display validation result
    return result.isValid();
}
Also used : ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) ValidationResult(org.apache.pdfbox.preflight.ValidationResult) PreflightDocument(org.apache.pdfbox.preflight.PreflightDocument)

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