Search in sources :

Example 11 with ArrayProperty

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

the class PdfaExtensionHelper method populateSchemaMapping.

public static void populateSchemaMapping(XMPMetadata meta) throws XmpParsingException {
    List<XMPSchema> schems = meta.getAllSchemas();
    TypeMapping tm = meta.getTypeMapping();
    StructuredType stPdfaExt = PDFAExtensionSchema.class.getAnnotation(StructuredType.class);
    for (XMPSchema xmpSchema : schems) {
        if (xmpSchema.getNamespace().equals(stPdfaExt.namespace())) {
            // ensure the prefix is the preferred one (cannot use other definition)
            if (!xmpSchema.getPrefix().equals(stPdfaExt.preferedPrefix())) {
                throw new XmpParsingException(ErrorType.InvalidPrefix, "Found invalid prefix for PDF/A extension, found '" + xmpSchema.getPrefix() + "', should be '" + stPdfaExt.preferedPrefix() + "'");
            }
            // create schema and types
            PDFAExtensionSchema pes = (PDFAExtensionSchema) xmpSchema;
            ArrayProperty sp = pes.getSchemasProperty();
            for (AbstractField af : sp.getAllProperties()) {
                if (af instanceof PDFASchemaType) {
                    populatePDFASchemaType(meta, (PDFASchemaType) af, tm);
                }
            // TODO unmanaged ?
            }
        }
    }
}
Also used : ArrayProperty(org.apache.xmpbox.type.ArrayProperty) AbstractField(org.apache.xmpbox.type.AbstractField) XMPSchema(org.apache.xmpbox.schema.XMPSchema) PDFAExtensionSchema(org.apache.xmpbox.schema.PDFAExtensionSchema) TypeMapping(org.apache.xmpbox.type.TypeMapping) PDFASchemaType(org.apache.xmpbox.type.PDFASchemaType) StructuredType(org.apache.xmpbox.type.StructuredType) AbstractStructuredType(org.apache.xmpbox.type.AbstractStructuredType) DefinedStructuredType(org.apache.xmpbox.type.DefinedStructuredType)

Example 12 with ArrayProperty

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

the class PdfaExtensionHelper method populatePDFASchemaType.

private static void populatePDFASchemaType(XMPMetadata meta, PDFASchemaType st, TypeMapping tm) throws XmpParsingException {
    String namespaceUri = st.getNamespaceURI().trim();
    String prefix = st.getPrefixValue();
    ArrayProperty properties = st.getProperty();
    ArrayProperty valueTypes = st.getValueType();
    XMPSchemaFactory xsf = tm.getSchemaFactory(namespaceUri);
    // retrieve namespaces
    if (xsf == null) {
        // create namespace with no field
        tm.addNewNameSpace(namespaceUri, prefix);
        xsf = tm.getSchemaFactory(namespaceUri);
    }
    // populate value type
    if (valueTypes != null) {
        for (AbstractField af2 : valueTypes.getAllProperties()) {
            if (af2 instanceof PDFATypeType) {
                populatePDFAType(meta, (PDFATypeType) af2, tm);
            }
        }
    }
    // populate properties
    if (properties == null) {
        throw new XmpParsingException(ErrorType.RequiredProperty, "Missing pdfaSchema:property in type definition");
    }
    for (AbstractField af2 : properties.getAllProperties()) {
        if (af2 instanceof PDFAPropertyType) {
            populatePDFAPropertyType((PDFAPropertyType) af2, tm, xsf);
        }
    // TODO unmanaged ?
    }
}
Also used : PDFATypeType(org.apache.xmpbox.type.PDFATypeType) ArrayProperty(org.apache.xmpbox.type.ArrayProperty) AbstractField(org.apache.xmpbox.type.AbstractField) XMPSchemaFactory(org.apache.xmpbox.schema.XMPSchemaFactory) PDFAPropertyType(org.apache.xmpbox.type.PDFAPropertyType)

Example 13 with ArrayProperty

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

the class XmpSerializer method serializeFields.

public void serializeFields(Document doc, Element parent, List<AbstractField> fields, String resourceNS, String prefix, boolean wrapWithProperty) {
    for (AbstractField field : fields) {
        if (field instanceof AbstractSimpleProperty) {
            AbstractSimpleProperty simple = (AbstractSimpleProperty) field;
            String localPrefix;
            if (prefix != null && !prefix.isEmpty()) {
                localPrefix = prefix;
            } else {
                localPrefix = simple.getPrefix();
            }
            Element esimple = doc.createElement(localPrefix + ":" + simple.getPropertyName());
            esimple.setTextContent(simple.getStringValue());
            List<Attribute> attributes = simple.getAllAttributes();
            for (Attribute attribute : attributes) {
                esimple.setAttributeNS(attribute.getNamespace(), attribute.getName(), attribute.getValue());
            }
            parent.appendChild(esimple);
        } else if (field instanceof ArrayProperty) {
            ArrayProperty array = (ArrayProperty) field;
            // property
            Element asimple = doc.createElement(array.getPrefix() + ":" + array.getPropertyName());
            parent.appendChild(asimple);
            // attributes
            fillElementWithAttributes(asimple, array);
            // the array definition
            Element econtainer = doc.createElement(XmpConstants.DEFAULT_RDF_PREFIX + ":" + array.getArrayType());
            asimple.appendChild(econtainer);
            // for each element of the array
            List<AbstractField> innerFields = array.getAllProperties();
            serializeFields(doc, econtainer, innerFields, resourceNS, XmpConstants.DEFAULT_RDF_PREFIX, false);
        } else if (field instanceof AbstractStructuredType) {
            AbstractStructuredType structured = (AbstractStructuredType) field;
            List<AbstractField> innerFields = structured.getAllProperties();
            // property name attribute
            Element listParent = parent;
            if (wrapWithProperty) {
                Element nstructured = doc.createElement(resourceNS + ":" + structured.getPropertyName());
                parent.appendChild(nstructured);
                listParent = nstructured;
            }
            // element li
            Element estructured = doc.createElement(XmpConstants.DEFAULT_RDF_PREFIX + ":" + XmpConstants.LIST_NAME);
            listParent.appendChild(estructured);
            if (parseTypeResourceForLi) {
                estructured.setAttribute("rdf:parseType", "Resource");
                // all properties
                serializeFields(doc, estructured, innerFields, resourceNS, null, true);
            } else {
                // element description
                Element econtainer = doc.createElement(XmpConstants.DEFAULT_RDF_PREFIX + ":" + "Description");
                estructured.appendChild(econtainer);
                // all properties
                serializeFields(doc, econtainer, innerFields, resourceNS, null, true);
            }
        } else {
            // XXX finish serialization classes
            System.err.println(">> TODO >> " + field.getClass());
        }
    }
}
Also used : ArrayProperty(org.apache.xmpbox.type.ArrayProperty) AbstractField(org.apache.xmpbox.type.AbstractField) Attribute(org.apache.xmpbox.type.Attribute) AbstractSimpleProperty(org.apache.xmpbox.type.AbstractSimpleProperty) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) List(java.util.List) AbstractStructuredType(org.apache.xmpbox.type.AbstractStructuredType)

Example 14 with ArrayProperty

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

the class AbstractSchemaTester method internalTestPropertySetterInArray.

private void internalTestPropertySetterInArray() throws Exception {
    if (cardinality == Cardinality.Simple) {
        return;
    }
    XMPSchema schema = getSchema();
    // add value
    String setter = "add" + calculateFieldNameForMethod(fieldName);
    // TypeDescription<AbstractSimpleProperty> td =
    // typeMapping.getSimpleDescription(type);
    Object value1 = getJavaValue(type);
    Method set = getSchemaClass().getMethod(setter, getJavaType(type));
    set.invoke(schema, value1);
    // retrieve complex property
    String getter = calculateArrayGetter(fieldName) + "Property";
    Method getcp = getSchemaClass().getMethod(getter);
    Object ocp = getcp.invoke(schema);
    Assert.assertTrue(ocp instanceof ArrayProperty);
    ArrayProperty cp = (ArrayProperty) ocp;
    // check size is ok (1)
    Assert.assertEquals(1, cp.getContainer().getAllProperties().size());
    // add a new one
    Object value2 = getJavaValue(type);
    set.invoke(schema, value2);
    Assert.assertEquals(2, cp.getContainer().getAllProperties().size());
    // remove the first
    String remover = "remove" + calculateFieldNameForMethod(fieldName);
    Method remove = getSchemaClass().getMethod(remover, getJavaType(type));
    remove.invoke(schema, value1);
    Assert.assertEquals(1, cp.getContainer().getAllProperties().size());
}
Also used : ArrayProperty(org.apache.xmpbox.type.ArrayProperty) Method(java.lang.reflect.Method)

Example 15 with ArrayProperty

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

the class XMPSchema method getUnqualifiedSequenceDateValueList.

/**
 * Get all the date values in a sequence property.
 *
 * @param seqName
 *            The name of the sequence property, it must include the namespace prefix, e.g. "pdf:Keywords".
 *
 * @return A read-only list of java.util.Calendar objects or null if the property does not exist.
 */
public List<Calendar> getUnqualifiedSequenceDateValueList(String seqName) {
    List<Calendar> retval = null;
    ArrayProperty seq = (ArrayProperty) getAbstractProperty(seqName);
    if (seq != null) {
        retval = new ArrayList<>();
        for (AbstractField child : seq.getContainer().getAllProperties()) {
            if (child instanceof DateType) {
                retval.add(((DateType) child).getValue());
            }
        }
    }
    return retval;
}
Also used : ArrayProperty(org.apache.xmpbox.type.ArrayProperty) AbstractField(org.apache.xmpbox.type.AbstractField) Calendar(java.util.Calendar) DateType(org.apache.xmpbox.type.DateType)

Aggregations

ArrayProperty (org.apache.xmpbox.type.ArrayProperty)17 AbstractField (org.apache.xmpbox.type.AbstractField)11 TextType (org.apache.xmpbox.type.TextType)5 Attribute (org.apache.xmpbox.type.Attribute)4 TypeMapping (org.apache.xmpbox.type.TypeMapping)4 ArrayList (java.util.ArrayList)3 AbstractStructuredType (org.apache.xmpbox.type.AbstractStructuredType)3 Element (org.w3c.dom.Element)3 List (java.util.List)2 AbstractSimpleProperty (org.apache.xmpbox.type.AbstractSimpleProperty)2 DefinedStructuredType (org.apache.xmpbox.type.DefinedStructuredType)2 PDFAPropertyType (org.apache.xmpbox.type.PDFAPropertyType)2 PropertiesDescription (org.apache.xmpbox.type.PropertiesDescription)2 PropertyType (org.apache.xmpbox.type.PropertyType)2 Method (java.lang.reflect.Method)1 Calendar (java.util.Calendar)1 Map (java.util.Map)1 QName (javax.xml.namespace.QName)1 ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)1 XMPMetadata (org.apache.xmpbox.XMPMetadata)1