use of eu.esdihumboldt.hale.common.scripting.Script in project hale by halestudio.
the class AbstractScriptedPropertyTransformation method evaluate.
@Override
protected final ListMultimap<String, Object> evaluate(String transformationIdentifier, E engine, ListMultimap<String, PropertyValue> variables, ListMultimap<String, PropertyEntityDefinition> resultNames, Map<String, String> executionParameters, TransformationLog log) throws TransformationException {
ListMultimap<String, ParameterValue> originalParameters = getParameters();
ListMultimap<String, Value> transformedParameters = ArrayListMultimap.create();
if (originalParameters != null) {
for (Map.Entry<String, ParameterValue> entry : originalParameters.entries()) {
if (!entry.getValue().needsProcessing()) {
Value value = entry.getValue().intern();
if (!value.isRepresentedAsDOM()) {
value = Value.simple(getExecutionContext().getVariables().replaceVariables(value.getStringRepresentation()));
}
transformedParameters.put(entry.getKey(), value);
} else {
// type is a script
ScriptFactory factory = ScriptExtension.getInstance().getFactory(entry.getValue().getType());
if (factory == null)
throw new TransformationException("Couldn't find factory for script id " + entry.getValue().getType());
Script script;
try {
script = factory.createExtensionObject();
} catch (Exception e) {
throw new TransformationException("Couldn't create script from factory", e);
}
Object result;
try {
String scriptStr = entry.getValue().as(String.class);
if (script.requiresReplacedTransformationVariables()) {
// replace transformation variables
scriptStr = getExecutionContext().getVariables().replaceVariables(scriptStr);
}
result = script.evaluate(scriptStr, variables.values(), getExecutionContext());
} catch (ScriptException e) {
throw new TransformationException("Couldn't evaluate a transformation parameter", e);
}
// XXX use conversion service instead of valueOf?
transformedParameters.put(entry.getKey(), Value.simple(result));
}
}
}
this.transformedParameters = Multimaps.unmodifiableListMultimap(transformedParameters);
return evaluateImpl(transformationIdentifier, engine, variables, resultNames, executionParameters, log);
}
use of eu.esdihumboldt.hale.common.scripting.Script in project hale by halestudio.
the class GroovyScript method evaluate.
/**
* @see Script#evaluate(String, Iterable, ServiceProvider)
*/
@Override
public Object evaluate(String script, Iterable<PropertyValue> variables, ServiceProvider provider) throws ScriptException {
Binding binding = createGroovyBinding(variables, true);
GroovyService service = provider.getService(GroovyService.class);
Object result;
try {
result = service.evaluate(service.parseScript(script, binding), new ResultProcessor<Object>() {
@Override
public Object process(groovy.lang.Script script, Object returnValue) throws Exception {
return returnValue;
}
});
} catch (Exception e) {
throw new ScriptException(e);
} catch (Throwable t) {
throw new ScriptException(t.toString());
}
if (result == null) {
// XXX throw new NoResultException(); ? as cause for SE?
throw new ScriptException("no result");
}
return result;
}
Aggregations