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