use of groovy.util.DelegatingScript in project RecordManager2 by moravianlibrary.
the class AbstractScriptFactory method innerCreate.
private MappingScript<T> innerCreate(List<byte[]> scriptsSource) {
logger.info("About to create new mapping script");
final AssignmentEliminationTransformation astTransformer = new AssignmentEliminationTransformation();
final Binding binding = createBinding();
final List<DelegatingScript> scripts = new ArrayList<DelegatingScript>(scriptsSource.size());
final CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
compilerConfiguration.setScriptBaseClass(DelegatingScript.class.getCanonicalName());
compilerConfiguration.addCompilationCustomizers(new SecureASTCustomizer(), new ASTTransformationCustomizer(astTransformer));
for (byte[] scriptSource : scriptsSource) {
GroovyShell sh = new GroovyShell(binding, compilerConfiguration);
final DelegatingScript script = (DelegatingScript) sh.parse(new InputStreamReader(new ByteArrayInputStream(scriptSource)));
scripts.add(script);
astTransformer.getVariablesToExclude().addAll(astTransformer.getVariablesToExclude());
}
Collections.reverse(scripts);
MappingScript<T> result = create(binding, scripts);
logger.info("New mapping script created");
return result;
}
use of groovy.util.DelegatingScript in project mdw-designer by CenturyLinkCloud.
the class GroovyTestCaseScript method substitute.
/**
* performs groovy substitutions with this as delegate and inherited
* bindings
*/
protected String substitute(String before) {
if (before.indexOf("${") == -1)
return before;
// escape all $ not followed by curly braces on same line
before = before.replaceAll("\\$(?!\\{)", "\\\\\\$");
// escape all regex -> ${~
before = before.replaceAll("\\$\\{~", "\\\\\\$\\{~");
// escape all escaped newlines
before = before.replaceAll("\\\\n", "\\\\\\\\\\n");
before = before.replaceAll("\\\\r", "\\\\\\\\\\r");
// escape all escaped quotes
before = before.replaceAll("\\\"", "\\\\\"");
CompilerConfiguration compilerCfg = new CompilerConfiguration();
compilerCfg.setScriptBaseClass(DelegatingScript.class.getName());
GroovyShell shell = new GroovyShell(GroovyTestCaseScript.class.getClassLoader(), getBinding(), compilerCfg);
DelegatingScript script = (DelegatingScript) shell.parse("return \"\"\"" + before + "\"\"\"");
script.setDelegate(GroovyTestCaseScript.this);
// restore escaped \$ to $ for comparison
return script.run().toString().replaceAll("\\\\$", "\\$");
}
Aggregations