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;
}
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;
}
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;
}
}
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();
}
}
Aggregations