Search in sources :

Example 1 with ValidationException

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

the class ZUGFeRDExporterFromA1Factory method getA1ParserValidationResult.

private static boolean getA1ParserValidationResult(PreflightParser parser) throws IOException {
    PreflightDocument document = 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.
			 */
        document = parser.getPreflightDocument();
        document.validate();
        // Get validation result
        return document.getResult().isValid();
    } 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;
    } finally {
        if (document != null) {
            document.close();
        }
    }
}
Also used : ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) PreflightDocument(org.apache.pdfbox.preflight.PreflightDocument)

Example 2 with ValidationException

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

the class AnnotationValidatorFactory method getAnnotationValidator.

/**
 * Return an instance of AnnotationValidator.
 *
 * @param ctx the preflight context.
 * @param annotDic the annotation dictionary.
 * @return the AnnotationValidator instance.
 */
public final AnnotationValidator getAnnotationValidator(PreflightContext ctx, COSDictionary annotDic) throws ValidationException {
    AnnotationValidator result = null;
    String subtype = annotDic.getNameAsString(COSName.SUBTYPE);
    Class<? extends AnnotationValidator> clazz = this.validatorClasses.get(subtype);
    if (clazz == null) {
        ctx.addValidationError(new ValidationError(ERROR_ANNOT_FORBIDDEN_SUBTYPE, "The subtype isn't authorized : " + subtype));
    } else {
        try {
            Constructor<? extends AnnotationValidator> constructor = clazz.getDeclaredConstructor(PreflightContext.class, COSDictionary.class);
            result = constructor.newInstance(ctx, annotDic);
            result.setFactory(this);
        } catch (Exception e) {
            throw new ValidationException(e.getMessage(), e);
        }
    }
    return result;
}
Also used : ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) ValidationException(org.apache.pdfbox.preflight.exception.ValidationException)

Example 3 with ValidationException

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

the class RDFAboutAttributeConcordanceValidation method validateRDFAboutAttributes.

/**
 * @param metadata the XMP metadata.
 * @throws DifferentRDFAboutException
 * @throws ValidationException
 */
public void validateRDFAboutAttributes(XMPMetadata metadata) throws ValidationException, DifferentRDFAboutException {
    List<XMPSchema> schemas = metadata.getAllSchemas();
    if (schemas.isEmpty()) {
        throw new ValidationException("Schemas not found in the given metadata representation");
    }
    String about = schemas.get(0).getAboutValue();
    // rdf:description must have an rdf:about attribute
    for (XMPSchema xmpSchema : schemas) {
        // each rdf:Description must have the same rdf:about (or an empty one)
        String schemaAboutValue = xmpSchema.getAboutValue();
        if (!("".equals(schemaAboutValue) || "".equals(about) || about.equals(schemaAboutValue))) {
            throw new DifferentRDFAboutException();
        }
        if ("".equals(about)) {
            about = schemaAboutValue;
        }
    }
}
Also used : ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) XMPSchema(org.apache.xmpbox.schema.XMPSchema)

Example 4 with ValidationException

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

the class SynchronizedMetaDataValidation method validateMetadataSynchronization.

/**
 * Check if document information entries and XMP information are synchronized
 *
 * @param document the PDF Document
 * @param metadata the XMP MetaData
 * @return List of validation errors
 * @throws ValidationException
 */
public List<ValidationError> validateMetadataSynchronization(PDDocument document, XMPMetadata metadata) throws ValidationException {
    List<ValidationError> ve = new ArrayList<>();
    if (document == null) {
        throw new ValidationException("Document provided is null");
    } else {
        PDDocumentInformation dico = document.getDocumentInformation();
        if (metadata == null) {
            throw new ValidationException("Metadata provided are null");
        } else {
            DublinCoreSchema dc = metadata.getDublinCoreSchema();
            // TITLE
            analyzeTitleProperty(dico, dc, ve);
            // AUTHOR
            analyzeAuthorProperty(dico, dc, ve);
            // SUBJECT
            analyzeSubjectProperty(dico, dc, ve);
            AdobePDFSchema pdf = metadata.getAdobePDFSchema();
            // KEYWORDS
            analyzeKeywordsProperty(dico, pdf, ve);
            // PRODUCER
            analyzeProducerProperty(dico, pdf, ve);
            XMPBasicSchema xmp = metadata.getXMPBasicSchema();
            // CREATOR TOOL
            analyzeCreatorToolProperty(dico, xmp, ve);
            // CREATION DATE
            analyzeCreationDateProperty(dico, xmp, ve);
            // MODIFY DATE
            analyzeModifyDateProperty(dico, xmp, ve);
        }
    }
    return ve;
}
Also used : ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) ArrayList(java.util.ArrayList) XMPBasicSchema(org.apache.xmpbox.schema.XMPBasicSchema) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) DublinCoreSchema(org.apache.xmpbox.schema.DublinCoreSchema) AdobePDFSchema(org.apache.xmpbox.schema.AdobePDFSchema) PDDocumentInformation(org.apache.pdfbox.pdmodel.PDDocumentInformation)

Example 5 with ValidationException

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

the class CatalogValidationProcess method validateICCProfile.

/**
 * This method checks the destOutputProfile which must be a valid ICCProfile.
 *
 * If an other ICCProfile exists in the mapDestOutputProfile, a ValdiationError
 * (ERROR_GRAPHIC_OUTPUT_INTENT_ICC_PROFILE_MULTIPLE) is returned because of only one profile is authorized. If the
 * ICCProfile already exist in the mapDestOutputProfile, the method returns null. If the destOutputProfile contains
 * an invalid ICCProfile, a ValidationError (ERROR_GRAPHIC_OUTPUT_INTENT_ICC_PROFILE_INVALID) is returned If the
 * destOutputProfile is an empty stream, a ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_INVALID_ENTRY) is returned.
 *
 * If the destOutputFile is valid, mapDestOutputProfile is updated, the ICCProfile is added to the document ctx and
 * null is returned.
 *
 * @param destOutputProfile
 * @param mapDestOutputProfile
 * @param ctx the preflight context.
 * @throws ValidationException
 */
protected void validateICCProfile(COSBase destOutputProfile, Map<COSObjectKey, Boolean> mapDestOutputProfile, PreflightContext ctx) throws ValidationException {
    try {
        if (destOutputProfile == null) {
            return;
        }
        // destOutputProfile should be an instance of COSObject because of this is a object reference
        if (destOutputProfile instanceof COSObject) {
            if (mapDestOutputProfile.containsKey(new COSObjectKey((COSObject) destOutputProfile))) {
                // the profile is already checked. continue
                return;
            } else if (!mapDestOutputProfile.isEmpty()) {
                // A DestOutputProfile exits but it isn't the same, error
                addValidationError(ctx, new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_ICC_PROFILE_MULTIPLE, "More than one ICCProfile is defined"));
                return;
            }
        // else the profile will be kept in the tmpDestOutputProfile if it is valid
        }
        // keep reference to avoid multiple profile definition
        mapDestOutputProfile.put(new COSObjectKey((COSObject) destOutputProfile), true);
        COSDocument cosDocument = ctx.getDocument().getDocument();
        COSStream stream = COSUtils.getAsStream(destOutputProfile, cosDocument);
        if (stream == null) {
            addValidationError(ctx, new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_INVALID_ENTRY, "OutputIntent object uses a NULL Object"));
            return;
        }
        InputStream is = stream.createInputStream();
        ICC_Profile iccp = null;
        try {
            iccp = ICC_Profile.getInstance(is);
        } finally {
            is.close();
        }
        if (!validateICCProfileNEntry(stream, ctx, iccp)) {
            return;
        }
        if (!validateICCProfileVersion(iccp, ctx)) {
            return;
        }
        if (ctx.getIccProfileWrapper() == null) {
            ctx.setIccProfileWrapper(new ICCProfileWrapper(iccp));
        }
    } catch (IllegalArgumentException e) {
        // this is not a ICC_Profile
        addValidationError(ctx, new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_ICC_PROFILE_INVALID, "DestOutputProfile isn't a valid ICCProfile: " + e.getMessage(), e));
    } catch (IOException e) {
        throw new ValidationException("Unable to parse the ICC Profile.", e);
    }
}
Also used : COSObjectKey(org.apache.pdfbox.cos.COSObjectKey) COSStream(org.apache.pdfbox.cos.COSStream) ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) COSObject(org.apache.pdfbox.cos.COSObject) InputStream(java.io.InputStream) COSDocument(org.apache.pdfbox.cos.COSDocument) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) IOException(java.io.IOException) ICC_Profile(java.awt.color.ICC_Profile) ICCProfileWrapper(org.apache.pdfbox.preflight.graphic.ICCProfileWrapper)

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