Search in sources :

Example 1 with XMPSchema

use of org.apache.xmpbox.schema.XMPSchema 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 2 with XMPSchema

use of org.apache.xmpbox.schema.XMPSchema in project pdfbox by apache.

the class TestValidatePermitedMetadata method checkExistence.

@Test
public void checkExistence() throws Exception {
    // ensure schema exists
    XMPMetadata xmpmd = new XMPMetadata();
    TypeMapping mapping = new TypeMapping(xmpmd);
    XMPSchemaFactory factory = mapping.getSchemaFactory(namespace);
    assertNotNull("Schema not existing: " + namespace, factory);
    // ensure preferred is as expected
    XMPSchema schema = factory.createXMPSchema(xmpmd, "aa");
    assertEquals(preferred, schema.getPreferedPrefix());
    // ensure field is defined
    boolean found = false;
    Class<?> clz = schema.getClass();
    for (Field dfield : clz.getDeclaredFields()) {
        PropertyType ptype = dfield.getAnnotation(PropertyType.class);
        if (ptype != null) {
            // is a field definition
            if (String.class.equals(dfield.getType())) {
                String value = (String) dfield.get(clz);
                if (fieldname.equals(value)) {
                    // found the field defining
                    found = true;
                    break;
                }
            } else {
                // All field declaration are string
                throw new IllegalArgumentException("Should be a string : " + dfield.getName());
            }
        }
    }
    String msg = String.format("Did not find field definition for '%s' in %s (%s)", fieldname, clz.getSimpleName(), namespace);
    assertTrue(msg, found);
}
Also used : Field(java.lang.reflect.Field) XMPSchemaFactory(org.apache.xmpbox.schema.XMPSchemaFactory) XMPSchema(org.apache.xmpbox.schema.XMPSchema) TypeMapping(org.apache.xmpbox.type.TypeMapping) PropertyType(org.apache.xmpbox.type.PropertyType) Test(org.junit.Test)

Example 3 with XMPSchema

use of org.apache.xmpbox.schema.XMPSchema in project pdfbox by apache.

the class DeserializationTest method testRdfAboutFound.

@Test
public void testRdfAboutFound() throws Exception {
    InputStream fis = DomXmpParser.class.getResourceAsStream("/validxmp/emptyli.xml");
    DomXmpParser xdb = new DomXmpParser();
    XMPMetadata meta = xdb.parse(fis);
    List<XMPSchema> schemas = meta.getAllSchemas();
    for (XMPSchema xmpSchema : schemas) {
        Assert.assertNotNull(xmpSchema.getAboutAttribute());
    }
}
Also used : XMPMetadata(org.apache.xmpbox.XMPMetadata) XMPSchema(org.apache.xmpbox.schema.XMPSchema) InputStream(java.io.InputStream) DomXmpParser(org.apache.xmpbox.xml.DomXmpParser) Test(org.junit.Test)

Example 4 with XMPSchema

use of org.apache.xmpbox.schema.XMPSchema in project pdfbox by apache.

the class DomXmpParser method parseDescriptionRootAttr.

private void parseDescriptionRootAttr(XMPMetadata xmp, Element description, Attr attr, TypeMapping tm) throws XmpSchemaException, XmpParsingException {
    String namespace = attr.getNamespaceURI();
    XMPSchema schema = xmp.getSchema(namespace);
    if (schema == null && tm.getSchemaFactory(namespace) != null) {
        schema = tm.getSchemaFactory(namespace).createXMPSchema(xmp, attr.getPrefix());
        loadAttributes(schema, description);
    }
    // Only process when a schema was successfully found
    if (schema != null) {
        ComplexPropertyContainer container = schema.getContainer();
        PropertyType type = checkPropertyDefinition(xmp, new QName(attr.getNamespaceURI(), attr.getLocalName()));
        // Default to text if no type is found
        if (type == null) {
            type = TypeMapping.createPropertyType(Types.Text, Cardinality.Simple);
        }
        try {
            AbstractSimpleProperty sp = tm.instanciateSimpleProperty(namespace, schema.getPrefix(), attr.getLocalName(), attr.getValue(), type.type());
            container.addProperty(sp);
        } catch (IllegalArgumentException e) {
            throw new XmpParsingException(ErrorType.Format, e.getMessage() + " in " + schema.getPrefix() + ":" + attr.getLocalName(), e);
        }
    }
}
Also used : XMPSchema(org.apache.xmpbox.schema.XMPSchema) QName(javax.xml.namespace.QName) AbstractSimpleProperty(org.apache.xmpbox.type.AbstractSimpleProperty) ComplexPropertyContainer(org.apache.xmpbox.type.ComplexPropertyContainer) PropertyType(org.apache.xmpbox.type.PropertyType)

Example 5 with XMPSchema

use of org.apache.xmpbox.schema.XMPSchema in project pdfbox by apache.

the class DomXmpParser method parseChildrenAsProperties.

private void parseChildrenAsProperties(XMPMetadata xmp, List<Element> properties, TypeMapping tm, Element description) throws XmpParsingException, XmpSchemaException {
    // parse children elements as properties
    for (Element property : properties) {
        String namespace = property.getNamespaceURI();
        PropertyType type = checkPropertyDefinition(xmp, DomHelper.getQName(property));
        // create the container
        if (!tm.isDefinedSchema(namespace)) {
            throw new XmpParsingException(ErrorType.NoSchema, "This namespace is not a schema or a structured type : " + namespace);
        }
        XMPSchema schema = xmp.getSchema(namespace);
        if (schema == null) {
            schema = tm.getSchemaFactory(namespace).createXMPSchema(xmp, property.getPrefix());
            loadAttributes(schema, description);
        }
        ComplexPropertyContainer container = schema.getContainer();
        // create property
        createProperty(xmp, property, type, container);
    }
}
Also used : XMPSchema(org.apache.xmpbox.schema.XMPSchema) Element(org.w3c.dom.Element) ComplexPropertyContainer(org.apache.xmpbox.type.ComplexPropertyContainer) PropertyType(org.apache.xmpbox.type.PropertyType)

Aggregations

XMPSchema (org.apache.xmpbox.schema.XMPSchema)11 PropertyType (org.apache.xmpbox.type.PropertyType)3 Test (org.junit.Test)3 ComplexPropertyContainer (org.apache.xmpbox.type.ComplexPropertyContainer)2 StructuredType (org.apache.xmpbox.type.StructuredType)2 TypeMapping (org.apache.xmpbox.type.TypeMapping)2 Element (org.w3c.dom.Element)2 InputStream (java.io.InputStream)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 QName (javax.xml.namespace.QName)1 ValidationException (org.apache.pdfbox.preflight.exception.ValidationException)1 XMPMetadata (org.apache.xmpbox.XMPMetadata)1 DublinCoreSchema (org.apache.xmpbox.schema.DublinCoreSchema)1 PDFAExtensionSchema (org.apache.xmpbox.schema.PDFAExtensionSchema)1 XMPSchemaFactory (org.apache.xmpbox.schema.XMPSchemaFactory)1 AbstractField (org.apache.xmpbox.type.AbstractField)1 AbstractSimpleProperty (org.apache.xmpbox.type.AbstractSimpleProperty)1 AbstractStructuredType (org.apache.xmpbox.type.AbstractStructuredType)1 ArrayProperty (org.apache.xmpbox.type.ArrayProperty)1