Search in sources :

Example 1 with DelegatingScript

use of groovy.util.DelegatingScript in project hale by halestudio.

the class SchemaBuilderReader method execute.

@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    progress.begin("Run schema builder", ProgressIndicator.UNKNOWN);
    try {
        CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
        compilerConfiguration.setScriptBaseClass(DelegatingScript.class.getName());
        // Configure the GroovyShell and pass the compiler configuration.
        GroovyShell shell = new GroovyShell(getClass().getClassLoader(), new Binding(), compilerConfiguration);
        DelegatingScript script;
        try (InputStream in = getSource().getInput();
            InputStreamReader reader = new InputStreamReader(in, getCharset())) {
            script = (DelegatingScript) shell.parse(reader);
        }
        SchemaBuilder builder = new SchemaBuilder();
        script.setDelegate(builder);
        Object res = script.run();
        if (res == null) {
            throw new IllegalStateException("Null returned by script");
        } else if (res instanceof Schema) {
            schema = (Schema) res;
        } else if (res instanceof TypeIndex) {
            DefaultSchema s = new DefaultSchema(null, getSource().getLocation());
            for (TypeDefinition type : ((TypeIndex) res).getTypes()) {
                s.addType(type);
            }
            schema = s;
        } else if (res instanceof TypeDefinition) {
            DefaultSchema s = new DefaultSchema(null, getSource().getLocation());
            s.addType((TypeDefinition) res);
            schema = s;
        } else {
            throw new IllegalStateException("Unrecognised return type: " + res.getClass().getName());
        }
        reporter.setSuccess(true);
    } catch (Exception e) {
        reporter.setSuccess(false);
        reporter.error("Error running schema builder", e);
    } finally {
        progress.end();
    }
    return reporter;
}
Also used : Binding(groovy.lang.Binding) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) DefaultSchema(eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchema) Schema(eu.esdihumboldt.hale.common.schema.model.Schema) GroovyShell(groovy.lang.GroovyShell) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) TypeIndex(eu.esdihumboldt.hale.common.schema.model.TypeIndex) DelegatingScript(groovy.util.DelegatingScript) DefaultSchema(eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchema) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) SchemaBuilder(eu.esdihumboldt.hale.common.schema.groovy.SchemaBuilder)

Example 2 with DelegatingScript

use of groovy.util.DelegatingScript in project RecordManager2 by moravianlibrary.

the class MarcMappingScriptImpl method parse.

@Override
public Map<String, Object> parse(MarcFunctionContext ctx) {
    binding.getVariables().clear();
    MarcDSL delegate = new MarcDSL(ctx, propertyResolver, stopWordsResolver, functions);
    for (DelegatingScript script : scripts) {
        script.setDelegate(delegate);
        script.run();
    }
    @SuppressWarnings("unchecked") Map<String, Object> entries = (Map<String, Object>) binding.getVariables();
    Map<String, Object> copy = new HashMap<>(entries);
    return copy;
}
Also used : HashMap(java.util.HashMap) DelegatingScript(groovy.util.DelegatingScript) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with DelegatingScript

use of groovy.util.DelegatingScript in project mdw-designer by CenturyLinkCloud.

the class GroovyTestCaseScript method adapter.

/**
 * responder closure call is delayed until stub server calls back
 */
public TestCaseAdapterStub adapter(Closure<Boolean> matcher, Closure<String> responder, Closure<?> init) throws TestException {
    final TestCaseAdapterStub adapterStub = new TestCaseAdapterStub(matcher, responder);
    if (init != null) {
        init.setResolveStrategy(Closure.DELEGATE_FIRST);
        init.setDelegate(adapterStub);
        init.call();
    }
    if (responder == null) {
        final TestCaseRun testCaseRun = getTestCaseRun();
        adapterStub.setResponder(new Closure<String>(this, adapterStub) {

            @Override
            public String call(Object request) {
                // binding for request
                if (adapterStub.getResponse().indexOf("${") >= 0) {
                    try {
                        Binding binding = getBinding();
                        if (request.toString().startsWith("{")) {
                            Object req = new JsonSlurper().parseText(request.toString());
                            binding.setVariable("request", req);
                        } else {
                            GPathResult gpathRequest = new XmlSlurper().parseText(request.toString());
                            binding.setVariable("request", gpathRequest);
                        }
                        CompilerConfiguration compilerCfg = new CompilerConfiguration();
                        compilerCfg.setScriptBaseClass(DelegatingScript.class.getName());
                        GroovyShell shell = new GroovyShell(GroovyTestCaseScript.class.getClassLoader(), binding, compilerCfg);
                        shell.setProperty("out", testCaseRun.log);
                        DelegatingScript script = (DelegatingScript) shell.parse("return \"\"\"" + adapterStub.getResponse() + "\"\"\"");
                        script.setDelegate(GroovyTestCaseScript.this);
                        return script.run().toString();
                    } catch (Exception ex) {
                        getTestCaseRun().log.println("Cannot perform stub substitutions for request: " + request);
                        ex.printStackTrace(getTestCaseRun().log);
                    }
                }
                return adapterStub.getResponse();
            }
        });
    }
    return adapterStub;
}
Also used : Binding(groovy.lang.Binding) JsonSlurper(groovy.json.JsonSlurper) XmlSlurper(groovy.util.XmlSlurper) GroovyShell(groovy.lang.GroovyShell) IOException(java.io.IOException) MbengException(com.qwest.mbeng.MbengException) DelegatingScript(groovy.util.DelegatingScript) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) JSONObject(org.json.JSONObject) GPathResult(groovy.util.slurpersupport.GPathResult)

Example 4 with DelegatingScript

use of groovy.util.DelegatingScript in project hale by halestudio.

the class InstanceBuilderReader method execute.

@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    progress.begin("Run instance builder", ProgressIndicator.UNKNOWN);
    try {
        CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
        compilerConfiguration.setScriptBaseClass(DelegatingScript.class.getName());
        // Configure the GroovyShell and pass the compiler configuration.
        GroovyShell shell = new GroovyShell(getClass().getClassLoader(), new Binding(), compilerConfiguration);
        DelegatingScript script;
        try (InputStream in = getSource().getInput();
            InputStreamReader reader = new InputStreamReader(in, getCharset())) {
            script = (DelegatingScript) shell.parse(reader);
        }
        InstanceBuilder builder = new InstanceBuilder();
        // apply schema
        builder.setTypes(getSourceSchema());
        script.setDelegate(builder);
        Object res = script.run();
        if (res == null) {
            throw new IllegalStateException("Null returned by script");
        } else if (res instanceof InstanceCollection) {
            instances = (InstanceCollection) res;
        } else if (res instanceof Instance) {
            instances = new DefaultInstanceCollection(Collections.singleton((Instance) res));
        } else {
            throw new IllegalStateException("Unrecognised return type: " + res.getClass().getName());
        }
        reporter.setSuccess(true);
    } catch (Exception e) {
        reporter.setSuccess(false);
        reporter.error("Error running instance builder", e);
    } finally {
        progress.end();
    }
    return reporter;
}
Also used : Binding(groovy.lang.Binding) InputStreamReader(java.io.InputStreamReader) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) InputStream(java.io.InputStream) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) GroovyShell(groovy.lang.GroovyShell) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException) DelegatingScript(groovy.util.DelegatingScript) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) InstanceBuilder(eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)

Example 5 with DelegatingScript

use of groovy.util.DelegatingScript in project RecordManager2 by moravianlibrary.

the class DublinCoreMappingScriptImpl method parse.

@Override
public Map<String, Object> parse(DublinCoreFunctionContext dcContext) {
    binding.getVariables().clear();
    DublinCoreDSL delegate = new DublinCoreDSL(dcContext, propertyResolver, stopWordsResolver, functions);
    for (DelegatingScript script : scripts) {
        script.setDelegate(delegate);
        script.run();
    }
    @SuppressWarnings("unchecked") Map<String, Object> entries = (Map<String, Object>) binding.getVariables();
    return entries;
}
Also used : DelegatingScript(groovy.util.DelegatingScript) Map(java.util.Map)

Aggregations

DelegatingScript (groovy.util.DelegatingScript)7 GroovyShell (groovy.lang.GroovyShell)5 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)5 Binding (groovy.lang.Binding)4 IOException (java.io.IOException)3 InputStreamReader (java.io.InputStreamReader)3 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)2 InputStream (java.io.InputStream)2 Map (java.util.Map)2 MbengException (com.qwest.mbeng.MbengException)1 InstanceBuilder (eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)1 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)1 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)1 DefaultInstanceCollection (eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection)1 SchemaBuilder (eu.esdihumboldt.hale.common.schema.groovy.SchemaBuilder)1 Schema (eu.esdihumboldt.hale.common.schema.model.Schema)1 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)1 TypeIndex (eu.esdihumboldt.hale.common.schema.model.TypeIndex)1 DefaultSchema (eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchema)1 JsonSlurper (groovy.json.JsonSlurper)1