Search in sources :

Example 11 with XmpParsingException

use of org.apache.xmpbox.xml.XmpParsingException in project pdfbox by apache.

the class DeserializationTest method testUndefinedStructuredWithDefinedSchema.

@Test
public void testUndefinedStructuredWithDefinedSchema() throws Exception {
    InputStream fis = DomXmpParser.class.getResourceAsStream("/invalidxmp/undefinedstructuredindefinedschema.xml");
    DomXmpParser xdb = new DomXmpParser();
    try {
        xdb.parse(fis);
        Assert.fail("Should fail during parse");
    } catch (XmpParsingException e) {
        Assert.assertEquals(ErrorType.NoValueType, e.getErrorType());
    }
}
Also used : XmpParsingException(org.apache.xmpbox.xml.XmpParsingException) InputStream(java.io.InputStream) DomXmpParser(org.apache.xmpbox.xml.DomXmpParser) Test(org.junit.Test)

Example 12 with XmpParsingException

use of org.apache.xmpbox.xml.XmpParsingException in project pdfbox by apache.

the class DeserializationTest method testWithNoXPacketEnd.

@Test
public void testWithNoXPacketEnd() throws Exception {
    InputStream fis = DomXmpParser.class.getResourceAsStream("/invalidxmp/noxpacketend.xml");
    DomXmpParser xdb = new DomXmpParser();
    try {
        xdb.parse(fis);
        Assert.fail("Should fail during parse");
    } catch (XmpParsingException e) {
        Assert.assertEquals(ErrorType.XpacketBadEnd, e.getErrorType());
    }
}
Also used : XmpParsingException(org.apache.xmpbox.xml.XmpParsingException) InputStream(java.io.InputStream) DomXmpParser(org.apache.xmpbox.xml.DomXmpParser) Test(org.junit.Test)

Example 13 with XmpParsingException

use of org.apache.xmpbox.xml.XmpParsingException 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)

Aggregations

DomXmpParser (org.apache.xmpbox.xml.DomXmpParser)13 XmpParsingException (org.apache.xmpbox.xml.XmpParsingException)13 InputStream (java.io.InputStream)11 Test (org.junit.Test)9 XMPMetadata (org.apache.xmpbox.XMPMetadata)4 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)3 PDMetadata (org.apache.pdfbox.pdmodel.common.PDMetadata)3 ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 PDDocumentCatalog (org.apache.pdfbox.pdmodel.PDDocumentCatalog)1 PDDocumentInformation (org.apache.pdfbox.pdmodel.PDDocumentInformation)1 ValidationException (org.apache.pdfbox.preflight.exception.ValidationException)1 FontMetaDataValidation (org.apache.pdfbox.preflight.font.util.FontMetaDataValidation)1 PDFAIdentificationValidation (org.apache.pdfbox.preflight.metadata.PDFAIdentificationValidation)1 RDFAboutAttributeConcordanceValidation (org.apache.pdfbox.preflight.metadata.RDFAboutAttributeConcordanceValidation)1 DifferentRDFAboutException (org.apache.pdfbox.preflight.metadata.RDFAboutAttributeConcordanceValidation.DifferentRDFAboutException)1 SynchronizedMetaDataValidation (org.apache.pdfbox.preflight.metadata.SynchronizedMetaDataValidation)1