Search in sources :

Example 1 with IOProvider

use of eu.esdihumboldt.hale.common.core.io.IOProvider in project hale by halestudio.

the class CharsetConfigurationPage method enable.

@Override
public void enable() {
    // select I/O provider default charset
    IOProvider pro = getWizard().getProvider();
    if (pro != null) {
        Charset cs = pro.getCharset();
        if (cs != null) {
            setCharset(cs.name());
            update();
        }
        if (detectButton != null) {
            detectButton.setEnabled(pro instanceof ImportProvider);
        }
    }
}
Also used : Charset(java.nio.charset.Charset) ImportProvider(eu.esdihumboldt.hale.common.core.io.ImportProvider) IOProvider(eu.esdihumboldt.hale.common.core.io.IOProvider)

Example 2 with IOProvider

use of eu.esdihumboldt.hale.common.core.io.IOProvider in project hale by halestudio.

the class HeadlessIO method executeConfiguration.

/**
 * Execute a single I/O configuration. If no matching advisor is given for
 * the configuration, first the extension point is queried for an advisor,
 * if not found it is ignored.
 *
 * @param conf the I/O configuration
 * @param advisors map of advisors, action ID mapped to responsible advisor
 * @param reportHandler the report handler, may be <code>null</code>
 * @param serviceProvider the service provider
 * @throws IOException if an error occurs executing a configuration
 */
public static void executeConfiguration(IOConfiguration conf, Map<String, IOAdvisor<?>> advisors, ReportHandler reportHandler, ServiceProvider serviceProvider) throws IOException {
    // get advisor ...
    final String actionId = conf.getActionId();
    IOAdvisor<?> advisor = advisors.get(actionId);
    if (advisor == null) {
        // try to find registered advisor for action (e.g. lookup)
        List<IOAdvisorFactory> regAdvisors = IOAdvisorExtension.getInstance().getFactories(new FactoryFilter<IOAdvisor<?>, IOAdvisorFactory>() {

            @Override
            public boolean acceptFactory(IOAdvisorFactory factory) {
                return factory.getActionID().equals(actionId);
            }

            @Override
            public boolean acceptCollection(ExtensionObjectFactoryCollection<IOAdvisor<?>, IOAdvisorFactory> collection) {
                return true;
            }
        });
        if (regAdvisors != null && !regAdvisors.isEmpty()) {
            try {
                advisor = regAdvisors.get(0).createAdvisor(actionId, serviceProvider);
                log.info(MessageFormat.format("No advisor for action {0} given, using advisor registered through extension point.", actionId));
            } catch (Exception e) {
                log.error(MessageFormat.format("Failed to load registered advisor for action {0}.", actionId));
                if (advisor != null) {
                    // shouldn't happen, but seems to happen anyway
                    log.error("Advisor is set in spite of error", e);
                }
            }
        }
    }
    if (advisor == null) {
        log.info(MessageFormat.format("No advisor for action {0} given, I/O configuration is ignored.", actionId));
        // ignore this configuration
        return;
    }
    // ... and provider
    IOProvider provider = loadProvider(conf);
    if (provider != null) {
        if (serviceProvider != null) {
            // set service provider, just in case the advisor does not do it
            provider.setServiceProvider(serviceProvider);
        }
        // execute provider
        executeProvider(provider, advisor, null, reportHandler);
    // XXX progress?!!
    } else {
        throw new IOException(MessageFormat.format("Could not execute I/O configuration, provider with ID {0} not found.", conf.getProviderId()));
    }
}
Also used : IOAdvisorFactory(eu.esdihumboldt.hale.common.core.io.extension.IOAdvisorFactory) IOAdvisor(eu.esdihumboldt.hale.common.core.io.IOAdvisor) IOProvider(eu.esdihumboldt.hale.common.core.io.IOProvider) IOException(java.io.IOException) IOException(java.io.IOException)

Example 3 with IOProvider

use of eu.esdihumboldt.hale.common.core.io.IOProvider in project hale by halestudio.

the class HeadlessIO method loadProvider.

/**
 * Load and configure the I/O provider specified by the given I/O
 * configuration.
 *
 * @param conf the I/O configuration
 * @return the provider or <code>null</code> if it was not found or could
 *         not be created
 */
public static IOProvider loadProvider(IOConfiguration conf) {
    IOProvider provider = null;
    IOProviderDescriptor descriptor = IOProviderExtension.getInstance().getFactory(conf.getProviderId());
    if (descriptor != null) {
        try {
            provider = descriptor.createExtensionObject();
            // configure settings
            provider.loadConfiguration(conf.getProviderConfiguration());
            if (provider instanceof CachingImportProvider) {
                ((CachingImportProvider) provider).setCache(conf.getCache());
            }
        } catch (Exception e) {
            log.error("Could not instantiate I/O provider.", e);
        }
    }
    return provider;
}
Also used : IOProviderDescriptor(eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor) CachingImportProvider(eu.esdihumboldt.hale.common.core.io.CachingImportProvider) IOProvider(eu.esdihumboldt.hale.common.core.io.IOProvider) IOException(java.io.IOException)

Example 4 with IOProvider

use of eu.esdihumboldt.hale.common.core.io.IOProvider in project hale by halestudio.

the class AppSchemaFileWriterTest method loadTestProject.

@BeforeClass
public static void loadTestProject() {
    try {
        URL archiveLocation = AppSchemaFileWriterTest.class.getResource(PROJECT_LOCATION);
        ArchiveProjectReader projectReader = new ArchiveProjectReader();
        projectReader.setSource(new DefaultInputSupplier(archiveLocation.toURI()));
        IOReport report = projectReader.execute(new LogProgressIndicator());
        if (!report.isSuccess()) {
            throw new RuntimeException("project reader execution failed");
        }
        tempDir = projectReader.getTemporaryFiles().iterator().next();
        project = projectReader.getProject();
        assertNotNull(project);
        sourceSchemaSpace = new DefaultSchemaSpace();
        targetSchemaSpace = new DefaultSchemaSpace();
        // load schemas
        List<IOConfiguration> resources = project.getResources();
        for (IOConfiguration resource : resources) {
            String actionId = resource.getActionId();
            String providerId = resource.getProviderId();
            // get provider
            IOProvider provider = null;
            IOProviderDescriptor descriptor = IOProviderExtension.getInstance().getFactory(providerId);
            if (descriptor == null) {
                throw new RuntimeException("Could not load I/O provider with ID: " + resource.getProviderId());
            }
            provider = descriptor.createExtensionObject();
            provider.loadConfiguration(resource.getProviderConfiguration());
            prepareProvider(provider, project, tempDir.toURI());
            IOReport providerReport = provider.execute(new LogProgressIndicator());
            if (!providerReport.isSuccess()) {
                throw new RuntimeException("I/O provider execution failed");
            }
            // TODO: could (should?) be done by an advisor
            if (provider instanceof SchemaReader) {
                Schema schema = ((SchemaReader) provider).getSchema();
                if (actionId.equals(SchemaIO.ACTION_LOAD_SOURCE_SCHEMA)) {
                    sourceSchemaSpace.addSchema(schema);
                } else if (actionId.equals(SchemaIO.ACTION_LOAD_TARGET_SCHEMA)) {
                    targetSchemaSpace.addSchema(schema);
                }
            }
        }
        // load alignment
        List<ProjectFileInfo> projectFiles = project.getProjectFiles();
        for (ProjectFileInfo projectFile : projectFiles) {
            if (projectFile.getName().equals(AlignmentIO.PROJECT_FILE_ALIGNMENT)) {
                AlignmentReader alignReader = new JaxbAlignmentReader();
                alignReader.setSource(new DefaultInputSupplier(projectFile.getLocation()));
                alignReader.setSourceSchema(sourceSchemaSpace);
                alignReader.setTargetSchema(targetSchemaSpace);
                alignReader.setPathUpdater(new PathUpdate(null, null));
                IOReport alignReport = alignReader.execute(new LogProgressIndicator());
                if (!alignReport.isSuccess()) {
                    throw new RuntimeException("alignment reader execution failed");
                }
                alignment = alignReader.getAlignment();
                assertNotNull(alignment);
                break;
            }
        }
    } catch (Exception e) {
        log.error("Exception occurred", e);
        fail("Test project could not be loaded: " + e.getMessage());
    }
}
Also used : JaxbAlignmentReader(eu.esdihumboldt.hale.common.align.io.impl.JaxbAlignmentReader) AlignmentReader(eu.esdihumboldt.hale.common.align.io.AlignmentReader) JaxbAlignmentReader(eu.esdihumboldt.hale.common.align.io.impl.JaxbAlignmentReader) SchemaReader(eu.esdihumboldt.hale.common.schema.io.SchemaReader) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) IOProviderDescriptor(eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor) IOConfiguration(eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration) Schema(eu.esdihumboldt.hale.common.schema.model.Schema) DefaultSchemaSpace(eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchemaSpace) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) LogProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator) URL(java.net.URL) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) ZipException(java.util.zip.ZipException) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException) ArchiveProjectReader(eu.esdihumboldt.hale.common.core.io.project.impl.ArchiveProjectReader) ProjectFileInfo(eu.esdihumboldt.hale.common.core.io.project.model.ProjectFileInfo) PathUpdate(eu.esdihumboldt.hale.common.core.io.PathUpdate) IOProvider(eu.esdihumboldt.hale.common.core.io.IOProvider) BeforeClass(org.junit.BeforeClass)

Example 5 with IOProvider

use of eu.esdihumboldt.hale.common.core.io.IOProvider in project hale by halestudio.

the class IOReferenceContent method getInputStream.

@Override
public InputStream getInputStream(String pluginID, String href, Locale locale) {
    // it is an I/O provider reference
    if (href.startsWith(IO_PROVIDERS_TOPIC_PATH)) {
        String providerId = href.substring(IO_PROVIDERS_TOPIC_PATH.length());
        // strip everything after a ?
        int ind = providerId.indexOf('?');
        if (ind >= 0) {
            providerId = providerId.substring(0, ind);
        }
        // strip the .*htm? ending
        if (providerId.endsWith("html") || providerId.endsWith("htm")) {
            providerId = providerId.substring(0, providerId.lastIndexOf('.'));
        }
        try {
            return getIOProviderContent(providerId);
        } catch (Exception e) {
            log.error("Error creating instance io info page.", e);
            return null;
        }
    } else // should be an I/O provider overview by type
    if (href.startsWith(OVERVIEW_TOPIC_PATH)) {
        // extract provider type name
        String providerType = href.substring(OVERVIEW_TOPIC_PATH.length());
        // strip everything after a ?
        int ind = providerType.indexOf('?');
        if (ind >= 0) {
            providerType = providerType.substring(0, ind);
        }
        // strip the .*htm? ending
        if (providerType.endsWith("html") || providerType.endsWith("htm")) {
            providerType = providerType.substring(0, providerType.lastIndexOf('.'));
        }
        Class<? extends IOProvider> providerClass = null;
        switch(providerType) {
            case "InstanceReader":
                providerClass = InstanceReader.class;
                break;
            case "InstanceWriter":
                providerClass = InstanceWriter.class;
                break;
            case "InstanceValidator":
                providerClass = InstanceValidator.class;
                break;
        }
        if (providerClass != null) {
            final Class<? extends IOProvider> provider = providerClass;
            try {
                return getContentFromTemplate("overview." + providerType, TEMPLATE_OVERVIEW, new Callable<VelocityContext>() {

                    @Override
                    public VelocityContext call() throws Exception {
                        VelocityContext context = new VelocityContext();
                        // getProviderFactorries returns
                        // Collection<IOProviderDescriptor>
                        Collection<IOProviderDescriptor> writer = HaleIO.getProviderFactories(provider);
                        context.put("providers", writer);
                        context.put("providerType", provider.getSimpleName());
                        return context;
                    }
                });
            } catch (Exception e) {
                log.error("Error creating provider overview", e);
                return null;
            }
        }
        return null;
    }
    return null;
}
Also used : InstanceReader(eu.esdihumboldt.hale.common.instance.io.InstanceReader) InstanceValidator(eu.esdihumboldt.hale.common.instance.io.InstanceValidator) InstanceWriter(eu.esdihumboldt.hale.common.instance.io.InstanceWriter) IOProviderDescriptor(eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor) VelocityContext(org.apache.velocity.VelocityContext) IOProvider(eu.esdihumboldt.hale.common.core.io.IOProvider) TransformerException(javax.xml.transform.TransformerException) Callable(java.util.concurrent.Callable)

Aggregations

IOProvider (eu.esdihumboldt.hale.common.core.io.IOProvider)8 IOProviderDescriptor (eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor)4 IOException (java.io.IOException)4 CachingImportProvider (eu.esdihumboldt.hale.common.core.io.CachingImportProvider)2 IOAdvisor (eu.esdihumboldt.hale.common.core.io.IOAdvisor)2 IOAdvisorFactory (eu.esdihumboldt.hale.common.core.io.extension.IOAdvisorFactory)2 InstanceReader (eu.esdihumboldt.hale.common.instance.io.InstanceReader)2 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)2 CSVConfiguration (eu.esdihumboldt.hale.io.csv.reader.internal.CSVConfiguration)2 QName (javax.xml.namespace.QName)2 AlignmentReader (eu.esdihumboldt.hale.common.align.io.AlignmentReader)1 JaxbAlignmentReader (eu.esdihumboldt.hale.common.align.io.impl.JaxbAlignmentReader)1 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)1 ImportProvider (eu.esdihumboldt.hale.common.core.io.ImportProvider)1 PathUpdate (eu.esdihumboldt.hale.common.core.io.PathUpdate)1 LogProgressIndicator (eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator)1 ArchiveProjectReader (eu.esdihumboldt.hale.common.core.io.project.impl.ArchiveProjectReader)1 IOConfiguration (eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration)1 ProjectFileInfo (eu.esdihumboldt.hale.common.core.io.project.model.ProjectFileInfo)1 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)1