Search in sources :

Example 16 with AbstractField

use of org.apache.xmpbox.type.AbstractField 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 17 with AbstractField

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

Example 18 with AbstractField

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

the class XMPSchema method reorganizeAltOrder.

/**
 * Method used to place the 'x-default' value in first in Language alternatives as said in xmp spec
 *
 * @param alt
 *            The property to reorganize
 */
public void reorganizeAltOrder(ComplexPropertyContainer alt) {
    Iterator<AbstractField> it = alt.getAllProperties().iterator();
    AbstractField xdefault = null;
    boolean xdefaultFound = false;
    // If alternatives contains x-default in first value
    if (it.hasNext() && it.next().getAttribute(XmpConstants.LANG_NAME).getValue().equals(XmpConstants.X_DEFAULT)) {
        return;
    }
    // Find the xdefault definition
    while (it.hasNext() && !xdefaultFound) {
        xdefault = it.next();
        if (xdefault.getAttribute(XmpConstants.LANG_NAME).getValue().equals(XmpConstants.X_DEFAULT)) {
            alt.removeProperty(xdefault);
            xdefaultFound = true;
        }
    }
    if (xdefaultFound) {
        it = alt.getAllProperties().iterator();
        List<AbstractField> reordered = new ArrayList<>();
        List<AbstractField> toDelete = new ArrayList<>();
        reordered.add(xdefault);
        while (it.hasNext()) {
            AbstractField tmp = it.next();
            reordered.add(tmp);
            toDelete.add(tmp);
        }
        for (AbstractField aToDelete : toDelete) {
            alt.removeProperty(aToDelete);
        }
        it = reordered.iterator();
        while (it.hasNext()) {
            alt.addProperty(it.next());
        }
    }
}
Also used : AbstractField(org.apache.xmpbox.type.AbstractField) ArrayList(java.util.ArrayList)

Aggregations

AbstractField (org.apache.xmpbox.type.AbstractField)18 ArrayProperty (org.apache.xmpbox.type.ArrayProperty)11 ArrayList (java.util.ArrayList)5 Attribute (org.apache.xmpbox.type.Attribute)5 TextType (org.apache.xmpbox.type.TextType)5 TypeMapping (org.apache.xmpbox.type.TypeMapping)5 Element (org.w3c.dom.Element)4 AbstractSimpleProperty (org.apache.xmpbox.type.AbstractSimpleProperty)3 AbstractStructuredType (org.apache.xmpbox.type.AbstractStructuredType)3 List (java.util.List)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 IOException (java.io.IOException)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