Search in sources :

Example 11 with AbstractField

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

the class DomXmpParser method manageArray.

private void manageArray(XMPMetadata xmp, Element property, PropertyType type, ComplexPropertyContainer container) throws XmpParsingException {
    TypeMapping tm = xmp.getTypeMapping();
    String prefix = property.getPrefix();
    String name = property.getLocalName();
    String namespace = property.getNamespaceURI();
    Element bagOrSeq = DomHelper.getUniqueElementChild(property);
    // ensure this is the good type of array
    if (bagOrSeq == null) {
        // not an array
        String whatFound = "nothing";
        if (property.getFirstChild() != null) {
            whatFound = property.getFirstChild().getClass().getName();
        }
        throw new XmpParsingException(ErrorType.Format, "Invalid array definition, expecting " + type.card() + " and found " + whatFound + " [prefix=" + prefix + "; name=" + name + "]");
    }
    if (!bagOrSeq.getLocalName().equals(type.card().name())) {
        // not the good array type
        throw new XmpParsingException(ErrorType.Format, "Invalid array type, expecting " + type.card() + " and found " + bagOrSeq.getLocalName() + " [prefix=" + prefix + "; name=" + name + "]");
    }
    ArrayProperty array = tm.createArrayProperty(namespace, prefix, name, type.card());
    container.addProperty(array);
    List<Element> lis = DomHelper.getElementChildren(bagOrSeq);
    for (Element element : lis) {
        QName propertyQName = new QName(element.getLocalName());
        AbstractField ast = parseLiElement(xmp, propertyQName, element, type.type());
        if (ast != null) {
            array.addProperty(ast);
        }
    }
}
Also used : ArrayProperty(org.apache.xmpbox.type.ArrayProperty) AbstractField(org.apache.xmpbox.type.AbstractField) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) TypeMapping(org.apache.xmpbox.type.TypeMapping)

Example 12 with AbstractField

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

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

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

the class XmpSerializer method serializeSchema.

protected Element serializeSchema(Document doc, XMPSchema schema) {
    // prepare schema
    Element selem = doc.createElementNS(XmpConstants.RDF_NAMESPACE, "rdf:Description");
    selem.setAttributeNS(XmpConstants.RDF_NAMESPACE, "rdf:about", schema.getAboutValue());
    selem.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:" + schema.getPrefix(), schema.getNamespace());
    // the other attributes
    fillElementWithAttributes(selem, schema);
    // the content
    List<AbstractField> fields = schema.getAllProperties();
    serializeFields(doc, selem, fields, schema.getPrefix(), null, true);
    // return created schema
    return selem;
}
Also used : AbstractField(org.apache.xmpbox.type.AbstractField) Element(org.w3c.dom.Element)

Example 15 with AbstractField

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

the class XmpSerializer method normalizeAttributes.

/**
 * Normalize the list of attributes.
 *
 * Attributes which match a schema property are serialized as child elements
 * so only return the ones which do not match a schema property
 *
 * @param property the property that needs to be inspected
 * @return the list of attributed for serializing
 */
private List<Attribute> normalizeAttributes(AbstractComplexProperty property) {
    List<Attribute> attributes = property.getAllAttributes();
    List<Attribute> toSerialize = new ArrayList<>();
    List<AbstractField> fields = property.getAllProperties();
    for (Attribute attribute : attributes) {
        boolean matchesField = false;
        for (AbstractField field : fields) {
            if (attribute.getName().compareTo(field.getPropertyName()) == 0) {
                matchesField = true;
                break;
            }
        }
        if (!matchesField) {
            toSerialize.add(attribute);
        }
    }
    return toSerialize;
}
Also used : AbstractField(org.apache.xmpbox.type.AbstractField) Attribute(org.apache.xmpbox.type.Attribute) 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