Search in sources :

Example 6 with DelegatingScript

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;
}
Also used : Binding(groovy.lang.Binding) ASTTransformationCustomizer(org.codehaus.groovy.control.customizers.ASTTransformationCustomizer) SecureASTCustomizer(org.codehaus.groovy.control.customizers.SecureASTCustomizer) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) GroovyShell(groovy.lang.GroovyShell) ByteArrayInputStream(java.io.ByteArrayInputStream) DelegatingScript(groovy.util.DelegatingScript) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration)

Example 7 with DelegatingScript

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("\\\\$", "\\$");
}
Also used : DelegatingScript(groovy.util.DelegatingScript) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) GroovyShell(groovy.lang.GroovyShell)

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