Search in sources :

Example 6 with AbstractSimpleProperty

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

the class DomXmpParser method parseLiElement.

private AbstractField parseLiElement(XMPMetadata xmp, QName descriptor, Element liElement, Types type) throws XmpParsingException {
    if (DomHelper.isParseTypeResource(liElement)) {
        return parseLiDescription(xmp, descriptor, liElement);
    }
    // will find rdf:Description
    Element liChild = DomHelper.getUniqueElementChild(liElement);
    if (liChild != null) {
        nsFinder.push(liChild);
        return parseLiDescription(xmp, descriptor, liChild);
    } else {
        // no child
        String text = liElement.getTextContent();
        TypeMapping tm = xmp.getTypeMapping();
        AbstractSimpleProperty sp = tm.instanciateSimpleProperty(descriptor.getNamespaceURI(), descriptor.getPrefix(), descriptor.getLocalPart(), text, type);
        loadAttributes(sp, liElement);
        return sp;
    }
}
Also used : Element(org.w3c.dom.Element) AbstractSimpleProperty(org.apache.xmpbox.type.AbstractSimpleProperty) TypeMapping(org.apache.xmpbox.type.TypeMapping)

Example 7 with AbstractSimpleProperty

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

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

the class AbstractSchemaTester method internalTestPropertySetterSimple.

private void internalTestPropertySetterSimple() throws Exception {
    if (cardinality != Cardinality.Simple) {
        return;
    }
    XMPSchema schema = getSchema();
    String setter = calculateSimpleSetter(fieldName) + "Property";
    Object value = getJavaValue(type);
    AbstractSimpleProperty asp = typeMapping.instanciateSimpleProperty(schema.getNamespace(), schema.getPrefix(), fieldName, value, type);
    Method set = getSchemaClass().getMethod(setter, type.getImplementingClass());
    set.invoke(schema, asp);
    // check property set
    AbstractSimpleProperty stored = (AbstractSimpleProperty) schema.getProperty(fieldName);
    Assert.assertEquals(value, stored.getValue());
    // check getter
    String getter = calculateSimpleGetter(fieldName) + "Property";
    Method get = getSchemaClass().getMethod(getter);
    Object result = get.invoke(schema);
    Assert.assertTrue(type.getImplementingClass().isAssignableFrom(result.getClass()));
    Assert.assertEquals(asp, result);
}
Also used : AbstractSimpleProperty(org.apache.xmpbox.type.AbstractSimpleProperty) Method(java.lang.reflect.Method)

Example 9 with AbstractSimpleProperty

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

the class AbstractSchemaTester method internalTestSettingValue.

private void internalTestSettingValue() throws Exception {
    if (cardinality != Cardinality.Simple) {
        return;
    }
    XMPSchema schema = getSchema();
    // only test simple properties
    Object value = getJavaValue(type);
    AbstractSimpleProperty property = schema.instanciateSimple(fieldName, value);
    schema.addProperty(property);
    String qn = getPropertyQualifiedName(fieldName);
    Assert.assertNotNull(schema.getProperty(fieldName));
    // check other properties not modified
    List<Field> fields = getXmpFields(getSchemaClass());
    for (Field field : fields) {
        // do not check the current name
        String fqn = getPropertyQualifiedName(field.get(null).toString());
        if (!fqn.equals(qn)) {
            Assert.assertNull(schema.getProperty(fqn));
        }
    }
}
Also used : Field(java.lang.reflect.Field) AbstractSimpleProperty(org.apache.xmpbox.type.AbstractSimpleProperty)

Aggregations

AbstractSimpleProperty (org.apache.xmpbox.type.AbstractSimpleProperty)9 TypeMapping (org.apache.xmpbox.type.TypeMapping)4 AbstractField (org.apache.xmpbox.type.AbstractField)3 Element (org.w3c.dom.Element)3 Field (java.lang.reflect.Field)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AbstractStructuredType (org.apache.xmpbox.type.AbstractStructuredType)2 ArrayProperty (org.apache.xmpbox.type.ArrayProperty)2 ComplexPropertyContainer (org.apache.xmpbox.type.ComplexPropertyContainer)2 PropertyType (org.apache.xmpbox.type.PropertyType)2 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 QName (javax.xml.namespace.QName)1 XMPSchema (org.apache.xmpbox.schema.XMPSchema)1 Attribute (org.apache.xmpbox.type.Attribute)1 BadFieldValueException (org.apache.xmpbox.type.BadFieldValueException)1 PropertiesDescription (org.apache.xmpbox.type.PropertiesDescription)1 Types (org.apache.xmpbox.type.Types)1 NodeList (org.w3c.dom.NodeList)1