Search in sources :

Example 11 with ValidationException

use of org.apache.pdfbox.preflight.exception.ValidationException in project pdfbox by apache.

the class TestSynchronizedMetadataValidation method TestDocumentWithoutInformation.

/**
 * Check the detection of a PDF document without any information
 *
 * @throws Exception
 */
@Test
public void TestDocumentWithoutInformation() throws Exception {
    try {
        ve = sync.validateMetadataSynchronization(doc, metadata);
        // Test without any information
        Assert.assertEquals(0, ve.size());
    } catch (ValidationException e) {
        throw new Exception(e.getMessage());
    }
}
Also used : ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) IOException(java.io.IOException) ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) Test(org.junit.Test)

Example 12 with ValidationException

use of org.apache.pdfbox.preflight.exception.ValidationException 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)

Example 13 with ValidationException

use of org.apache.pdfbox.preflight.exception.ValidationException in project pdfbox by apache.

the class MetadataValidationProcess method validate.

@Override
public void validate(PreflightContext ctx) throws ValidationException {
    try {
        PDDocument document = ctx.getDocument();
        InputStream is = getXpacket(document.getDocument());
        DomXmpParser builder = new DomXmpParser();
        XMPMetadata metadata = builder.parse(is);
        is.close();
        ctx.setMetadata(metadata);
        // 6.7.5 no deprecated attribute in xpacket processing instruction
        if (metadata.getXpacketBytes() != null) {
            addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_METADATA_XPACKET_DEPRECATED, "bytes attribute is forbidden"));
        }
        if (metadata.getXpacketEncoding() != null) {
            addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_METADATA_XPACKET_DEPRECATED, "encoding attribute is forbidden"));
        }
        checkThumbnails(ctx, metadata);
        // Call metadata synchronization checking
        addValidationErrors(ctx, new SynchronizedMetaDataValidation().validateMetadataSynchronization(document, metadata));
        // Call PDF/A Identifier checking
        addValidationErrors(ctx, new PDFAIdentificationValidation().validatePDFAIdentifer(metadata));
        // Call rdf:about checking
        try {
            new RDFAboutAttributeConcordanceValidation().validateRDFAboutAttributes(metadata);
        } catch (DifferentRDFAboutException e) {
            addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_METADATA_RDF_ABOUT_ATTRIBUTE_INEQUAL_VALUE, e.getMessage(), e));
        }
    } catch (XpacketParsingException e) {
        if (e.getError() != null) {
            addValidationError(ctx, e.getError());
        } else {
            addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_METADATA_MAIN, "Unexpected error", e));
        }
    } catch (XmpParsingException e) {
        if (e.getErrorType() == ErrorType.NoValueType) {
            addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_METADATA_UNKNOWN_VALUETYPE, e.getMessage(), e));
        } else if (e.getErrorType() == ErrorType.RequiredProperty) {
            addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_METADATA_PROPERTY_MISSING, e.getMessage(), e));
        } else if (e.getErrorType() == ErrorType.InvalidPrefix) {
            addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_METADATA_ABSENT_DESCRIPTION_SCHEMA, e.getMessage(), e));
        } else if (e.getErrorType() == ErrorType.InvalidType) {
            addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_METADATA_PROPERTY_UNKNOWN, e.getMessage(), e));
        } else if (e.getErrorType() == ErrorType.XpacketBadEnd) {
            throw new ValidationException("Unable to parse font metadata due to : " + e.getMessage(), e);
        } else if (e.getErrorType() == ErrorType.NoSchema) {
            addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_METADATA_ABSENT_DESCRIPTION_SCHEMA, e.getMessage(), e));
        } else if (e.getErrorType() == ErrorType.InvalidPdfaSchema) {
            addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_METADATA_WRONG_NS_URI, e.getMessage(), e));
        } else {
            addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_METADATA_FORMAT, e.getMessage(), e));
        }
    } catch (IOException e) {
        throw new ValidationException("Failed while validating", e);
    }
}
Also used : XpacketParsingException(org.apache.pdfbox.preflight.metadata.XpacketParsingException) ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) RDFAboutAttributeConcordanceValidation(org.apache.pdfbox.preflight.metadata.RDFAboutAttributeConcordanceValidation) XmpParsingException(org.apache.xmpbox.xml.XmpParsingException) SynchronizedMetaDataValidation(org.apache.pdfbox.preflight.metadata.SynchronizedMetaDataValidation) XMPMetadata(org.apache.xmpbox.XMPMetadata) DomXmpParser(org.apache.xmpbox.xml.DomXmpParser) DifferentRDFAboutException(org.apache.pdfbox.preflight.metadata.RDFAboutAttributeConcordanceValidation.DifferentRDFAboutException) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) PDFAIdentificationValidation(org.apache.pdfbox.preflight.metadata.PDFAIdentificationValidation)

Example 14 with ValidationException

use of org.apache.pdfbox.preflight.exception.ValidationException in project pdfbox by apache.

the class StreamValidationProcess method checkStreamLength.

protected void checkStreamLength(PreflightContext context, COSObject cObj) throws ValidationException {
    COSStream streamObj = (COSStream) cObj.getObject();
    int length = streamObj.getInt(COSName.LENGTH);
    InputStream ra = null;
    try {
        ra = context.getSource().getInputStream();
        Long offset = context.getDocument().getDocument().getXrefTable().get(new COSObjectKey(cObj));
        // ---- go to the beginning of the object
        long skipped = 0;
        if (offset != null) {
            while (skipped != offset) {
                long curSkip = ra.skip(offset - skipped);
                if (curSkip < 0) {
                    addValidationError(context, new ValidationError(ERROR_SYNTAX_STREAM_DAMAGED, "Unable to skip bytes in the PDFFile to check stream length"));
                    return;
                }
                skipped += curSkip;
            }
            // ---- go to the stream key word
            if (readUntilStream(ra)) {
                int c = ra.read();
                if (c == '\r') {
                    ra.read();
                }
                // else c is '\n' no more character to read
                // ---- Here is the true beginning of the Stream Content.
                // ---- Read the given length of bytes and check the 10 next bytes
                // ---- to see if there are endstream.
                byte[] buffer = new byte[1024];
                int nbBytesToRead = length;
                do {
                    int cr;
                    if (nbBytesToRead > buffer.length) {
                        cr = ra.read(buffer);
                    } else {
                        cr = ra.read(buffer, 0, nbBytesToRead);
                    }
                    if (cr == -1) {
                        addStreamLengthValidationError(context, cObj, length, "");
                        return;
                    } else {
                        nbBytesToRead -= cr;
                    }
                } while (nbBytesToRead > 0);
                int len = "endstream".length() + 2;
                byte[] buffer2 = new byte[len];
                for (int i = 0; i < len; ++i) {
                    buffer2[i] = (byte) ra.read();
                }
                // ---- check the content of 10 last characters
                String endStream = new String(buffer2, Charsets.ISO_8859_1);
                if (buffer2[0] == '\r' && buffer2[1] == '\n') {
                    if (!endStream.contains("endstream")) {
                        addStreamLengthValidationError(context, cObj, length, endStream);
                    }
                } else if (buffer2[0] == '\r' && buffer2[1] == 'e') {
                    if (!endStream.contains("endstream")) {
                        addStreamLengthValidationError(context, cObj, length, endStream);
                    }
                } else if (buffer2[0] == '\n' && buffer2[1] == 'e') {
                    if (!endStream.contains("endstream")) {
                        addStreamLengthValidationError(context, cObj, length, endStream);
                    }
                } else {
                    if (!endStream.startsWith("endStream")) {
                        addStreamLengthValidationError(context, cObj, length, endStream);
                    }
                }
            } else {
                addStreamLengthValidationError(context, cObj, length, "");
            }
        }
    } catch (IOException e) {
        throw new ValidationException("Unable to read a stream to validate: " + e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(ra);
    }
}
Also used : COSObjectKey(org.apache.pdfbox.cos.COSObjectKey) COSStream(org.apache.pdfbox.cos.COSStream) ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) InputStream(java.io.InputStream) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) IOException(java.io.IOException)

Example 15 with ValidationException

use of org.apache.pdfbox.preflight.exception.ValidationException 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)

Aggregations

ValidationException (org.apache.pdfbox.preflight.exception.ValidationException)21 IOException (java.io.IOException)13 Test (org.junit.Test)11 ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)8 DublinCoreSchema (org.apache.xmpbox.schema.DublinCoreSchema)7 AdobePDFSchema (org.apache.xmpbox.schema.AdobePDFSchema)6 XMPBasicSchema (org.apache.xmpbox.schema.XMPBasicSchema)6 PreflightDocument (org.apache.pdfbox.preflight.PreflightDocument)4 InputStream (java.io.InputStream)3 COSObject (org.apache.pdfbox.cos.COSObject)3 ValidationResult (org.apache.pdfbox.preflight.ValidationResult)3 ArrayList (java.util.ArrayList)2 GregorianCalendar (java.util.GregorianCalendar)2 COSDictionary (org.apache.pdfbox.cos.COSDictionary)2 COSDocument (org.apache.pdfbox.cos.COSDocument)2 COSObjectKey (org.apache.pdfbox.cos.COSObjectKey)2 COSStream (org.apache.pdfbox.cos.COSStream)2 PreflightParser (org.apache.pdfbox.preflight.parser.PreflightParser)2 ICC_Profile (java.awt.color.ICC_Profile)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1