Search in sources :

Example 1 with ElementType

use of eu.esdihumboldt.hale.common.schema.model.constraint.type.ElementType in project hale by halestudio.

the class SimpleTypeUtil method convertFromXml.

/**
 * Convert a string belonging to a XML simple type to the binding specified
 * by the given type definition.
 *
 * @param value the string value
 * @param type the type definition
 * @return <code>null</code> if the string was <code>null</code>, the
 *         converted object with the binding type if possible, otherwise the
 *         original string
 */
public static Object convertFromXml(String value, TypeDefinition type) {
    if (value == null) {
        return null;
    }
    Class<? extends XmlAnySimpleType> simpleType = getSimpleType(type);
    Class<?> binding = type.getConstraint(Binding.class).getBinding();
    if (List.class.isAssignableFrom(binding)) {
        // XXX also for collection
        // binding?
        // we are dealing with a simple type list
        // items separated by whitespace
        String[] elements = value.trim().split("\\s+");
        ElementType elementType = type.getConstraint(ElementType.class);
        Class<? extends XmlAnySimpleType> elementSimpleType = null;
        if (elementType.getDefinition() != null) {
            elementSimpleType = getSimpleType(elementType.getDefinition());
        }
        Class<?> elementBinding = elementType.getBinding();
        List<Object> result = new ArrayList<Object>();
        for (String element : elements) {
            Object convElement = convertFromXml(element, elementSimpleType, elementBinding);
            result.add(convElement);
        }
        return result;
    }
    // convert ordinary value
    return convertFromXml(value, simpleType, binding);
}
Also used : Binding(eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding) ElementType(eu.esdihumboldt.hale.common.schema.model.constraint.type.ElementType) ArrayList(java.util.ArrayList)

Example 2 with ElementType

use of eu.esdihumboldt.hale.common.schema.model.constraint.type.ElementType in project hale by halestudio.

the class TypeDefinitionElementTypeBindingSection method refresh.

/**
 * @see AbstractPropertySection#refresh()
 */
@Override
public void refresh() {
    Binding bind = getDefinition().getConstraint(Binding.class);
    if (Collection.class.isAssignableFrom(bind.getBinding())) {
        ElementType element = getDefinition().getConstraint(ElementType.class);
        elementType.setText(element.getBinding().getName());
    } else {
        // XXX should not be displayed
        elementType.setText("");
    }
    binding.setText(bind.getBinding().getName());
}
Also used : Binding(eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding) ElementType(eu.esdihumboldt.hale.common.schema.model.constraint.type.ElementType)

Example 3 with ElementType

use of eu.esdihumboldt.hale.common.schema.model.constraint.type.ElementType in project hale by halestudio.

the class BindingCondition method accept.

/**
 * @see EntityCondition#accept(Entity)
 */
@Override
public boolean accept(Type entity) {
    // default
    boolean to = true;
    switch(entity.getDefinition().getSchemaSpace()) {
        case SOURCE:
            to = false;
            break;
        case TARGET:
            to = true;
            break;
    }
    TypeDefinition type = entity.getDefinition().getDefinition();
    if (!type.getConstraint(HasValueFlag.class).isEnabled() && !type.getConstraint(AugmentedValueFlag.class).isEnabled()) {
        // whether defined in the schema or augmented
        return false;
    }
    // check binding
    Binding binding = type.getConstraint(Binding.class);
    if (isCompatibleClass(binding.getBinding(), to)) {
        return true;
    }
    // check element type
    if (allowCollection) {
        ElementType elementType = type.getConstraint(ElementType.class);
        if (isCompatibleClass(elementType.getBinding(), to)) {
            return true;
        }
    }
    // no check succeeded
    return false;
}
Also used : Binding(eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding) ElementType(eu.esdihumboldt.hale.common.schema.model.constraint.type.ElementType) AugmentedValueFlag(eu.esdihumboldt.hale.common.schema.model.constraint.type.AugmentedValueFlag) HasValueFlag(eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 4 with ElementType

use of eu.esdihumboldt.hale.common.schema.model.constraint.type.ElementType in project hale by halestudio.

the class FunctionExecutor method convert.

/**
 * Convert a value according to a target property entity definition.
 *
 * @param value the value to convert
 * @param propertyEntityDefinition the target property entity definition
 * @return the converted object
 * @throws ConversionException if an error occurs during conversion
 */
private Object convert(Object value, PropertyEntityDefinition propertyEntityDefinition) throws ConversionException {
    if (value == null) {
        return null;
    }
    PropertyDefinition def = propertyEntityDefinition.getDefinition();
    Binding binding = def.getPropertyType().getConstraint(Binding.class);
    Class<?> target = binding.getBinding();
    // special handling for Value
    if (value instanceof Value) {
        // try value's internal conversion
        Object result = ((Value) value).as(target);
        if (result != null) {
            return result;
        } else {
            // unwrap value
            value = ((Value) value).getValue();
            if (value == null) {
                return null;
            }
        }
    }
    if (target.isAssignableFrom(value.getClass())) {
        return value;
    }
    if (Collection.class.isAssignableFrom(target) && target.isAssignableFrom(List.class)) {
        // collection / list
        ElementType elementType = def.getPropertyType().getConstraint(ElementType.class);
        return ConversionUtil.getAsList(value, elementType.getBinding(), true);
    }
    return ConversionUtil.getAs(value, target);
}
Also used : Binding(eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding) ElementType(eu.esdihumboldt.hale.common.schema.model.constraint.type.ElementType) MultiValue(eu.esdihumboldt.cst.MultiValue) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) Value(eu.esdihumboldt.hale.common.core.io.Value) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)

Aggregations

Binding (eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding)4 ElementType (eu.esdihumboldt.hale.common.schema.model.constraint.type.ElementType)4 ArrayList (java.util.ArrayList)2 MultiValue (eu.esdihumboldt.cst.MultiValue)1 PropertyValue (eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)1 Value (eu.esdihumboldt.hale.common.core.io.Value)1 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)1 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)1 AugmentedValueFlag (eu.esdihumboldt.hale.common.schema.model.constraint.type.AugmentedValueFlag)1 HasValueFlag (eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag)1 Collection (java.util.Collection)1 List (java.util.List)1