Search in sources :

Example 26 with DomXmpParser

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

the class AdobePDFErrorsTest method initTempMetaData.

@Before
public void initTempMetaData() throws Exception {
    builder = new DomXmpParser();
    metadata = XMPMetadata.createXMPMetadata();
}
Also used : DomXmpParser(org.apache.xmpbox.xml.DomXmpParser) Before(org.junit.Before)

Example 27 with DomXmpParser

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

the class TestExifXmp method testNonStrict.

@Test
public void testNonStrict() throws Exception {
    InputStream is = this.getClass().getResourceAsStream("/validxmp/exif.xmp");
    DomXmpParser builder = new DomXmpParser();
    builder.setStrictParsing(false);
    XMPMetadata rxmp = builder.parse(is);
    ExifSchema schema = (ExifSchema) rxmp.getSchema(ExifSchema.class);
    TextType ss = (TextType) schema.getProperty(ExifSchema.SPECTRAL_SENSITIVITY);
    Assert.assertNotNull(ss);
    Assert.assertEquals("spectral sens value", ss.getValue());
}
Also used : XMPMetadata(org.apache.xmpbox.XMPMetadata) InputStream(java.io.InputStream) DomXmpParser(org.apache.xmpbox.xml.DomXmpParser) TextType(org.apache.xmpbox.type.TextType) Test(org.junit.Test)

Example 28 with DomXmpParser

use of org.apache.xmpbox.xml.DomXmpParser 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)28 InputStream (java.io.InputStream)21 Test (org.junit.Test)21 XmpParsingException (org.apache.xmpbox.xml.XmpParsingException)12 XMPMetadata (org.apache.xmpbox.XMPMetadata)11 DublinCoreSchema (org.apache.xmpbox.schema.DublinCoreSchema)6 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)3 PDMetadata (org.apache.pdfbox.pdmodel.common.PDMetadata)3 ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)3 XMPBasicSchema (org.apache.xmpbox.schema.XMPBasicSchema)3 File (java.io.File)2 PDDocumentCatalog (org.apache.pdfbox.pdmodel.PDDocumentCatalog)2 AdobePDFSchema (org.apache.xmpbox.schema.AdobePDFSchema)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 PDDocumentInformation (org.apache.pdfbox.pdmodel.PDDocumentInformation)1 PreflightDocument (org.apache.pdfbox.preflight.PreflightDocument)1