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