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());
}
}
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();
}
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);
}
}
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);
}
}
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();
}
}
}
Aggregations