Search in sources :

Example 1 with PropertyType

use of org.apache.xmpbox.type.PropertyType in project pdfbox by apache.

the class DomXmpParser method parseLiDescription.

private AbstractStructuredType parseLiDescription(XMPMetadata xmp, QName descriptor, Element liElement) throws XmpParsingException {
    TypeMapping tm = xmp.getTypeMapping();
    List<Element> elements = DomHelper.getElementChildren(liElement);
    if (elements.isEmpty()) {
        // The list is empty
        return null;
    }
    // Instantiate abstract structured type with hint from first element
    Element first = elements.get(0);
    PropertyType ctype = checkPropertyDefinition(xmp, DomHelper.getQName(first));
    Types tt = ctype.type();
    AbstractStructuredType ast = instanciateStructured(tm, tt, descriptor.getLocalPart(), first.getNamespaceURI());
    ast.setNamespace(descriptor.getNamespaceURI());
    ast.setPrefix(descriptor.getPrefix());
    PropertiesDescription pm;
    if (tt.isStructured()) {
        pm = tm.getStructuredPropMapping(tt);
    } else {
        pm = tm.getDefinedDescriptionByNamespace(first.getNamespaceURI());
    }
    for (Element element : elements) {
        String prefix = element.getPrefix();
        String name = element.getLocalName();
        String namespace = element.getNamespaceURI();
        PropertyType type = pm.getPropertyType(name);
        if (type == null) {
            // not defined
            throw new XmpParsingException(ErrorType.NoType, "Type '" + name + "' not defined in " + element.getNamespaceURI());
        } else if (type.card().isArray()) {
            ArrayProperty array = tm.createArrayProperty(namespace, prefix, name, type.card());
            ast.getContainer().addProperty(array);
            Element bagOrSeq = DomHelper.getUniqueElementChild(element);
            List<Element> lis = DomHelper.getElementChildren(bagOrSeq);
            for (Element element2 : lis) {
                AbstractField ast2 = parseLiElement(xmp, descriptor, element2, type.type());
                if (ast2 != null) {
                    array.addProperty(ast2);
                }
            }
        } else if (type.type().isSimple()) {
            AbstractSimpleProperty sp = tm.instanciateSimpleProperty(namespace, prefix, name, element.getTextContent(), type.type());
            loadAttributes(sp, element);
            ast.getContainer().addProperty(sp);
        } else if (type.type().isStructured()) {
            // create a new structured type
            AbstractStructuredType inner = instanciateStructured(tm, type.type(), name, null);
            inner.setNamespace(namespace);
            inner.setPrefix(prefix);
            ast.getContainer().addProperty(inner);
            ComplexPropertyContainer cpc = inner.getContainer();
            if (DomHelper.isParseTypeResource(element)) {
                parseDescriptionInner(xmp, element, cpc);
            } else {
                Element descElement = DomHelper.getFirstChildElement(element);
                if (descElement != null) {
                    parseDescriptionInner(xmp, descElement, cpc);
                }
            }
        } else {
            throw new XmpParsingException(ErrorType.NoType, "Unidentified element to parse " + element + " (type=" + type + ")");
        }
    }
    return ast;
}
Also used : Types(org.apache.xmpbox.type.Types) ArrayProperty(org.apache.xmpbox.type.ArrayProperty) AbstractField(org.apache.xmpbox.type.AbstractField) PropertiesDescription(org.apache.xmpbox.type.PropertiesDescription) Element(org.w3c.dom.Element) AbstractSimpleProperty(org.apache.xmpbox.type.AbstractSimpleProperty) ComplexPropertyContainer(org.apache.xmpbox.type.ComplexPropertyContainer) PropertyType(org.apache.xmpbox.type.PropertyType) TypeMapping(org.apache.xmpbox.type.TypeMapping) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) AbstractStructuredType(org.apache.xmpbox.type.AbstractStructuredType)

Example 2 with PropertyType

use of org.apache.xmpbox.type.PropertyType in project pdfbox by apache.

the class DomXmpParser method parseDescriptionInner.

private void parseDescriptionInner(XMPMetadata xmp, Element description, ComplexPropertyContainer parentContainer) throws XmpParsingException {
    nsFinder.push(description);
    TypeMapping tm = xmp.getTypeMapping();
    try {
        List<Element> properties = DomHelper.getElementChildren(description);
        for (Element property : properties) {
            String name = property.getLocalName();
            PropertyType dtype = checkPropertyDefinition(xmp, DomHelper.getQName(property));
            PropertyType ptype = tm.getStructuredPropMapping(dtype.type()).getPropertyType(name);
            // create property
            createProperty(xmp, property, ptype, parentContainer);
        }
    } finally {
        nsFinder.pop();
    }
}
Also used : Element(org.w3c.dom.Element) TypeMapping(org.apache.xmpbox.type.TypeMapping) PropertyType(org.apache.xmpbox.type.PropertyType)

Example 3 with PropertyType

use of org.apache.xmpbox.type.PropertyType in project pdfbox by apache.

the class PdfaExtensionHelper method populatePDFAType.

private static void populatePDFAType(XMPMetadata meta, PDFATypeType type, TypeMapping tm) throws XmpParsingException {
    String ttype = type.getType();
    String tns = type.getNamespaceURI();
    String tprefix = type.getPrefixValue();
    String tdescription = type.getDescription();
    ArrayProperty fields = type.getFields();
    if (ttype == null || tns == null || tprefix == null || tdescription == null) {
        // all fields are mandatory
        throw new XmpParsingException(ErrorType.RequiredProperty, "Missing field in type definition");
    }
    // create the structured type
    // TODO
    DefinedStructuredType structuredType = new DefinedStructuredType(meta, tns, tprefix, null);
    // maybe a name exists
    if (fields != null) {
        List<AbstractField> definedFields = fields.getAllProperties();
        for (AbstractField af3 : definedFields) {
            if (af3 instanceof PDFAFieldType) {
                populatePDFAFieldType((PDFAFieldType) af3, structuredType);
            }
        // else TODO
        }
    }
    // add the structured type to list
    PropertiesDescription pm = new PropertiesDescription();
    for (Map.Entry<String, PropertyType> entry : structuredType.getDefinedProperties().entrySet()) {
        pm.addNewProperty(entry.getKey(), entry.getValue());
    }
    tm.addToDefinedStructuredTypes(ttype, tns, pm);
}
Also used : ArrayProperty(org.apache.xmpbox.type.ArrayProperty) DefinedStructuredType(org.apache.xmpbox.type.DefinedStructuredType) AbstractField(org.apache.xmpbox.type.AbstractField) PropertiesDescription(org.apache.xmpbox.type.PropertiesDescription) PDFAFieldType(org.apache.xmpbox.type.PDFAFieldType) PDFAPropertyType(org.apache.xmpbox.type.PDFAPropertyType) PropertyType(org.apache.xmpbox.type.PropertyType) Map(java.util.Map) NamedNodeMap(org.w3c.dom.NamedNodeMap)

Example 4 with PropertyType

use of org.apache.xmpbox.type.PropertyType in project pdfbox by apache.

the class PdfaExtensionHelper method populatePDFAPropertyType.

private static void populatePDFAPropertyType(PDFAPropertyType property, TypeMapping tm, XMPSchemaFactory xsf) throws XmpParsingException {
    String pname = property.getName();
    String ptype = property.getValueType();
    String pdescription = property.getDescription();
    String pCategory = property.getCategory();
    // check all mandatory fields are OK
    if (pname == null || ptype == null || pdescription == null || pCategory == null) {
        // all fields are mandatory
        throw new XmpParsingException(ErrorType.RequiredProperty, "Missing field in property definition");
    }
    // check ptype existance
    PropertyType pt = transformValueType(tm, ptype);
    if (pt == null) {
        throw new XmpParsingException(ErrorType.NoValueType, "Unknown property value type : " + ptype);
    }
    if (pt.type() == null) {
        throw new XmpParsingException(ErrorType.NoValueType, "Type not defined : " + ptype);
    } else if (pt.type().isSimple() || pt.type().isStructured() || pt.type() == Types.DefinedType) {
        xsf.getPropertyDefinition().addNewProperty(pname, pt);
    } else {
        throw new XmpParsingException(ErrorType.NoValueType, "Type not defined : " + ptype);
    }
}
Also used : PDFAPropertyType(org.apache.xmpbox.type.PDFAPropertyType) PropertyType(org.apache.xmpbox.type.PropertyType)

Example 5 with PropertyType

use of org.apache.xmpbox.type.PropertyType 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)

Aggregations

PropertyType (org.apache.xmpbox.type.PropertyType)7 XMPSchema (org.apache.xmpbox.schema.XMPSchema)3 ComplexPropertyContainer (org.apache.xmpbox.type.ComplexPropertyContainer)3 TypeMapping (org.apache.xmpbox.type.TypeMapping)3 Element (org.w3c.dom.Element)3 AbstractField (org.apache.xmpbox.type.AbstractField)2 AbstractSimpleProperty (org.apache.xmpbox.type.AbstractSimpleProperty)2 ArrayProperty (org.apache.xmpbox.type.ArrayProperty)2 PDFAPropertyType (org.apache.xmpbox.type.PDFAPropertyType)2 PropertiesDescription (org.apache.xmpbox.type.PropertiesDescription)2 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 QName (javax.xml.namespace.QName)1 XMPSchemaFactory (org.apache.xmpbox.schema.XMPSchemaFactory)1 AbstractStructuredType (org.apache.xmpbox.type.AbstractStructuredType)1 DefinedStructuredType (org.apache.xmpbox.type.DefinedStructuredType)1 PDFAFieldType (org.apache.xmpbox.type.PDFAFieldType)1 Types (org.apache.xmpbox.type.Types)1