Search in sources :

Example 1 with TypeIndex

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

Example 2 with TypeIndex

use of eu.esdihumboldt.hale.common.schema.model.TypeIndex in project hale by halestudio.

the class EntityTypeIndexHierarchy method getElements.

/**
 * @see ITreeContentProvider#getElements(Object)
 */
@Override
public Object[] getElements(Object inputElement) {
    if (inputElement instanceof TypeIndex) {
        validTypes = new HashSet<TypeDefinition>();
        List<TypeEntityDefinition> roots = new ArrayList<TypeEntityDefinition>();
        Queue<TypeDefinition> types = new LinkedList<TypeDefinition>();
        if (onlyMappingRelevant)
            types.addAll(((TypeIndex) inputElement).getMappingRelevantTypes());
        else {
            for (TypeDefinition type : ((TypeIndex) inputElement).getTypes()) if (type.getConstraint(MappableFlag.class).isEnabled())
                types.add(type);
        }
        // collect types and super types in valid types set
        while (!types.isEmpty()) {
            TypeDefinition type = types.poll();
            if (!validTypes.contains(type)) {
                validTypes.add(type);
                TypeDefinition superType = type.getSuperType();
                if (superType != null && !validTypes.contains(superType)) {
                    types.add(superType);
                }
                if (superType == null) {
                    // add default type as root
                    roots.add(new TypeEntityDefinition(type, schemaSpace, null));
                }
            }
        }
        // }
        return roots.toArray();
    } else {
        throw new IllegalArgumentException("Content provider only applicable for type indexes.");
    }
}
Also used : TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) TypeIndex(eu.esdihumboldt.hale.common.schema.model.TypeIndex) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Aggregations

TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)2 TypeIndex (eu.esdihumboldt.hale.common.schema.model.TypeIndex)2 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)1 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 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 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)1