Search in sources :

Example 1 with ComplexValueDefinition

use of eu.esdihumboldt.hale.common.core.io.extension.ComplexValueDefinition in project hale by halestudio.

the class HaleIO method getComplexValue.

/**
 * Get the value of a complex property represented as a DOM element.
 *
 * @param element the DOM element
 * @param context the context object, may be <code>null</code>
 * @return the complex value converted through the
 *         {@link ComplexValueExtension}, or the original element
 */
public static Object getComplexValue(Element element, Object context) {
    QName name;
    if (element.getNamespaceURI() != null && !element.getNamespaceURI().isEmpty()) {
        name = new QName(element.getNamespaceURI(), element.getLocalName());
    } else {
        // .getLocalName());
        name = new QName(element.getTagName());
    }
    ComplexValueDefinition cvt = ComplexValueExtension.getInstance().getDefinition(name);
    if (cvt != null) {
        // create and return the complex parameter value
        return cvt.fromDOM(element, cvt.getContextType().isInstance(context) ? context : null);
    }
    // the element itself is the complex value
    return element;
}
Also used : ComplexValueDefinition(eu.esdihumboldt.hale.common.core.io.extension.ComplexValueDefinition) QName(javax.xml.namespace.QName)

Example 2 with ComplexValueDefinition

use of eu.esdihumboldt.hale.common.core.io.extension.ComplexValueDefinition in project hale by halestudio.

the class HaleIO method getComplexValue.

/**
 * Get the value of a complex property represented as a DOM element.
 *
 * @param element the DOM element
 * @param expectedType the expected parameter type, this must be either
 *            {@link String}, DOM {@link Element} or a complex value type
 *            defined in the {@link ComplexValueExtension}
 * @param context the context object, may be <code>null</code>
 * @return the complex value or <code>null</code> if it could not be created
 *         from the element
 */
@SuppressWarnings("unchecked")
public static <T> T getComplexValue(Element element, Class<T> expectedType, Object context) {
    if (element == null) {
        return null;
    }
    QName name;
    if (element.getNamespaceURI() != null && !element.getNamespaceURI().isEmpty()) {
        name = new QName(element.getNamespaceURI(), element.getLocalName());
    } else {
        // .getLocalName();
        String ln = element.getTagName();
        name = new QName(ln);
    }
    ComplexValueDefinition cvt = ComplexValueExtension.getInstance().getDefinition(name);
    Object value = null;
    if (cvt != null) {
        try {
            value = cvt.fromDOM(element, cvt.getContextType().isInstance(context) ? context : null);
        } catch (Exception e) {
            throw new IllegalStateException("Failed to load complex value from DOM", e);
        }
    }
    if (value != null && expectedType.isAssignableFrom(value.getClass())) {
        return (T) value;
    }
    // maybe the element itself is OK
    if (expectedType.isAssignableFrom(element.getClass())) {
        return (T) element;
    }
    if (expectedType.isAssignableFrom(String.class)) {
    // FIXME use legacy complex value if possible
    }
    return null;
}
Also used : ComplexValueDefinition(eu.esdihumboldt.hale.common.core.io.extension.ComplexValueDefinition) QName(javax.xml.namespace.QName) IOException(java.io.IOException)

Example 3 with ComplexValueDefinition

use of eu.esdihumboldt.hale.common.core.io.extension.ComplexValueDefinition in project hale by halestudio.

the class ParameterUtil method getBinding.

/**
 * Determine the binding of a defined parameter.
 *
 * @param parameterConf the configuration element defining the parameter
 * @return the binding class or <code>null</code>
 */
@Nullable
public static Class<?> getBinding(IConfigurationElement parameterConf) {
    IConfigurationElement[] bindingElement = parameterConf.getChildren(CONF_PARAMETER_BINDING);
    IConfigurationElement[] enumerationElement = parameterConf.getChildren(CONF_PARAMETER_ENUMERATION);
    IConfigurationElement[] complexValueElement = parameterConf.getChildren(CONF_PARAMETER_COMPLEX_VALUE);
    if (bindingElement.length > 0) {
        // default to String
        String clazz = bindingElement[0].getAttribute("class");
        if (clazz == null)
            return String.class;
        else
            return ExtensionUtil.loadClass(bindingElement[0], "class");
    } else if (enumerationElement.length > 0) {
        return String.class;
    } else if (complexValueElement.length > 0) {
        String complexValueId = complexValueElement[0].getAttribute("ref");
        if (complexValueId != null) {
            ComplexValueDefinition cv = ComplexValueExtension.getInstance().get(complexValueId);
            if (cv != null) {
                return cv.getValueType();
            }
        }
        return null;
    } else {
        // default
        return String.class;
    }
}
Also used : ComplexValueDefinition(eu.esdihumboldt.hale.common.core.io.extension.ComplexValueDefinition) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) Nullable(javax.annotation.Nullable)

Example 4 with ComplexValueDefinition

use of eu.esdihumboldt.hale.common.core.io.extension.ComplexValueDefinition in project hale by halestudio.

the class JsonValueRepresentation method getValueRepresentation.

@Override
public Object getValueRepresentation(Value value) {
    if (value == null) {
        return null;
    }
    if (value.isRepresentedAsDOM()) {
        // try conversion using complex value mechanism
        Object intern = value.getValue();
        if (intern == null) {
            return null;
        }
        ComplexValueDefinition cdv = ComplexValueExtension.getInstance().getDefinition(intern.getClass());
        if (cdv != null && cdv.getJsonConverter() != null) {
            return cdv.getJsonConverter().toJson(intern);
        }
        // fall-back to generic XML-JSON conversion
        Element element = value.getDOMRepresentation();
        if (element == null) {
            return null;
        } else {
            String json = null;
            try {
                String xmlString = XmlUtil.serialize(element, false);
                try (StringReader xmlReader = new StringReader(xmlString);
                    StringWriter jsonWriter = new StringWriter()) {
                    JsonXML.toJson(xmlReader, jsonWriter);
                    json = jsonWriter.toString();
                }
            } catch (Exception e) {
                log.error("Failed to created JSON representation of value", e);
                return null;
            }
            Object res = new JsonSlurper().parseText(json);
            return res;
        }
    } else {
        return value.getStringRepresentation();
    }
}
Also used : JsonSlurper(groovy.json.JsonSlurper) StringWriter(java.io.StringWriter) ComplexValueDefinition(eu.esdihumboldt.hale.common.core.io.extension.ComplexValueDefinition) Element(org.w3c.dom.Element) StringReader(java.io.StringReader)

Aggregations

ComplexValueDefinition (eu.esdihumboldt.hale.common.core.io.extension.ComplexValueDefinition)4 QName (javax.xml.namespace.QName)2 JsonSlurper (groovy.json.JsonSlurper)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 Nullable (javax.annotation.Nullable)1 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)1 Element (org.w3c.dom.Element)1