Search in sources :

Example 1 with SchemaBuilder

use of eu.esdihumboldt.hale.common.schema.groovy.SchemaBuilder 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)

Aggregations

IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)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 Binding (groovy.lang.Binding)1 GroovyShell (groovy.lang.GroovyShell)1 DelegatingScript (groovy.util.DelegatingScript)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)1