Search in sources :

Example 1 with Script

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);
}
Also used : Script(eu.esdihumboldt.hale.common.scripting.Script) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) ScriptException(javax.script.ScriptException) ScriptException(javax.script.ScriptException) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) Value(eu.esdihumboldt.hale.common.core.io.Value) Map(java.util.Map) ScriptFactory(eu.esdihumboldt.hale.common.scripting.ScriptFactory)

Example 2 with Script

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;
}
Also used : Binding(groovy.lang.Binding) Script(eu.esdihumboldt.hale.common.scripting.Script) ScriptException(javax.script.ScriptException) ResultProcessor(eu.esdihumboldt.util.groovy.sandbox.GroovyService.ResultProcessor) GroovyService(eu.esdihumboldt.util.groovy.sandbox.GroovyService) ConversionException(org.springframework.core.convert.ConversionException) MissingPropertyException(groovy.lang.MissingPropertyException) ScriptException(javax.script.ScriptException)

Aggregations

Script (eu.esdihumboldt.hale.common.scripting.Script)2 ScriptException (javax.script.ScriptException)2 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)1 PropertyValue (eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)1 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)1 Value (eu.esdihumboldt.hale.common.core.io.Value)1 ScriptFactory (eu.esdihumboldt.hale.common.scripting.ScriptFactory)1 GroovyService (eu.esdihumboldt.util.groovy.sandbox.GroovyService)1 ResultProcessor (eu.esdihumboldt.util.groovy.sandbox.GroovyService.ResultProcessor)1 Binding (groovy.lang.Binding)1 MissingPropertyException (groovy.lang.MissingPropertyException)1 Map (java.util.Map)1 ConversionException (org.springframework.core.convert.ConversionException)1