Search in sources :

Example 1 with AbstractSimpleProperty

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

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

the class DomXmpParser method manageSimpleType.

private void manageSimpleType(XMPMetadata xmp, Element property, Types type, ComplexPropertyContainer container) throws XmpParsingException {
    TypeMapping tm = xmp.getTypeMapping();
    String prefix = property.getPrefix();
    String name = property.getLocalName();
    String namespace = property.getNamespaceURI();
    AbstractSimpleProperty sp = tm.instanciateSimpleProperty(namespace, prefix, name, property.getTextContent(), type);
    loadAttributes(sp, property);
    container.addProperty(sp);
}
Also used : AbstractSimpleProperty(org.apache.xmpbox.type.AbstractSimpleProperty) TypeMapping(org.apache.xmpbox.type.TypeMapping)

Example 3 with AbstractSimpleProperty

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

the class XMPSchema method setSpecifiedSimpleTypeProperty.

private void setSpecifiedSimpleTypeProperty(Types type, String qualifiedName, Object propertyValue) {
    if (propertyValue == null) {
        // Search in properties to erase
        for (AbstractField child : getContainer().getAllProperties()) {
            if (child.getPropertyName().equals(qualifiedName)) {
                getContainer().removeProperty(child);
                return;
            }
        }
    } else {
        AbstractSimpleProperty specifiedTypeProperty;
        try {
            TypeMapping tm = getMetadata().getTypeMapping();
            specifiedTypeProperty = tm.instanciateSimpleProperty(null, getPrefix(), qualifiedName, propertyValue, type);
        } catch (Exception e) {
            throw new IllegalArgumentException("Failed to create property with the specified type given in parameters", e);
        }
        // Search in properties to erase
        for (AbstractField child : getAllProperties()) {
            if (child.getPropertyName().equals(qualifiedName)) {
                removeProperty(child);
                addProperty(specifiedTypeProperty);
                return;
            }
        }
        addProperty(specifiedTypeProperty);
    }
}
Also used : AbstractField(org.apache.xmpbox.type.AbstractField) AbstractSimpleProperty(org.apache.xmpbox.type.AbstractSimpleProperty) TypeMapping(org.apache.xmpbox.type.TypeMapping) IOException(java.io.IOException) BadFieldValueException(org.apache.xmpbox.type.BadFieldValueException)

Example 4 with AbstractSimpleProperty

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

the class AbstractSchemaTester method internalTestSettingValueInArray.

private void internalTestSettingValueInArray() throws Exception {
    if (cardinality == Cardinality.Simple) {
        return;
    }
    XMPSchema schema = getSchema();
    // only test array properties
    Object value = getJavaValue(type);
    AbstractSimpleProperty property = schema.instanciateSimple(fieldName, value);
    switch(cardinality) {
        case Seq:
            schema.addUnqualifiedSequenceValue(property.getPropertyName(), property);
            break;
        case Bag:
            schema.addBagValue(property.getPropertyName(), property);
            break;
        default:
            throw new Exception("Unexpected case in test : " + cardinality.name());
    }
    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)

Example 5 with AbstractSimpleProperty

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

the class DomXmpParser method parseDescriptionRootAttr.

private void parseDescriptionRootAttr(XMPMetadata xmp, Element description, Attr attr, TypeMapping tm) throws XmpSchemaException, XmpParsingException {
    String namespace = attr.getNamespaceURI();
    XMPSchema schema = xmp.getSchema(namespace);
    if (schema == null && tm.getSchemaFactory(namespace) != null) {
        schema = tm.getSchemaFactory(namespace).createXMPSchema(xmp, attr.getPrefix());
        loadAttributes(schema, description);
    }
    // Only process when a schema was successfully found
    if (schema != null) {
        ComplexPropertyContainer container = schema.getContainer();
        PropertyType type = checkPropertyDefinition(xmp, new QName(attr.getNamespaceURI(), attr.getLocalName()));
        // Default to text if no type is found
        if (type == null) {
            type = TypeMapping.createPropertyType(Types.Text, Cardinality.Simple);
        }
        try {
            AbstractSimpleProperty sp = tm.instanciateSimpleProperty(namespace, schema.getPrefix(), attr.getLocalName(), attr.getValue(), type.type());
            container.addProperty(sp);
        } catch (IllegalArgumentException e) {
            throw new XmpParsingException(ErrorType.Format, e.getMessage() + " in " + schema.getPrefix() + ":" + attr.getLocalName(), e);
        }
    }
}
Also used : XMPSchema(org.apache.xmpbox.schema.XMPSchema) QName(javax.xml.namespace.QName) AbstractSimpleProperty(org.apache.xmpbox.type.AbstractSimpleProperty) ComplexPropertyContainer(org.apache.xmpbox.type.ComplexPropertyContainer) PropertyType(org.apache.xmpbox.type.PropertyType)

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