Search in sources :

Example 36 with IOProviderConfigurationException

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

the class DefaultProjectWriter method execute.

/**
 * @see AbstractIOProvider#execute(ProgressIndicator, IOReporter)
 */
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    boolean separateProjectFiles = !archive || isUseSeparateFiles();
    URI targetLocation = getTarget().getLocation();
    File targetFile;
    try {
        targetFile = new File(targetLocation);
    } catch (Exception e) {
        if (!archive) {
            // cannot save as XML if it's not a file
            reporter.error(new IOMessageImpl("Could not determine project file path.", e));
            reporter.setSuccess(false);
            return reporter;
        }
        targetFile = null;
        // if it's not a file, we must save the project files inside the zip
        // stream
        separateProjectFiles = false;
    }
    int entries = 1;
    if (getProjectFiles() != null) {
        entries += getProjectFiles().size();
    }
    progress.begin("Save project", entries);
    // clear project file information that may already be contained in the
    // project
    getProject().getProjectFiles().clear();
    // files
    if (separateProjectFiles && targetFile != null) {
        for (Entry<String, ProjectFile> entry : getProjectFiles().entrySet()) {
            String name = entry.getKey();
            // determine target file for project file
            String projectFileName = targetFile.getName() + "." + name;
            final File pfile = new File(targetFile.getParentFile(), projectFileName);
            // the following line is basically
            // URI.create(escape(projectFileName))
            URI relativeProjectFile = targetFile.getParentFile().toURI().relativize(pfile.toURI());
            // add project file information to project
            getProject().getProjectFiles().add(new ProjectFileInfo(name, relativeProjectFile));
            // write entry
            ProjectFile file = entry.getValue();
            try {
                LocatableOutputSupplier<OutputStream> target = new LocatableOutputSupplier<OutputStream>() {

                    @Override
                    public OutputStream getOutput() throws IOException {
                        return new BufferedOutputStream(new FileOutputStream(pfile));
                    }

                    @Override
                    public URI getLocation() {
                        return pfile.toURI();
                    }
                };
                file.store(target);
            } catch (Exception e) {
                reporter.error(new IOMessageImpl("Error saving a project file.", e));
                reporter.setSuccess(false);
                return reporter;
            }
            progress.advance(1);
        }
    }
    updateRelativeResourcePaths(getProject().getResources(), getPreviousTarget(), targetLocation);
    if (archive) {
        // save to archive
        final ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(getTarget().getOutput()));
        try {
            // write main entry
            zip.putNextEntry(new ZipEntry(ProjectIO.PROJECT_FILE));
            try {
                Project.save(getProject(), new EntryOutputStream(zip));
            } catch (Exception e) {
                reporter.error(new IOMessageImpl("Could not save main project configuration.", e));
                reporter.setSuccess(false);
                return reporter;
            }
            zip.closeEntry();
            progress.advance(1);
            // write additional project files to zip stream
            if (getProjectFiles() != null && !separateProjectFiles) {
                for (Entry<String, ProjectFile> entry : getProjectFiles().entrySet()) {
                    String name = entry.getKey();
                    if (name.equalsIgnoreCase(ProjectIO.PROJECT_FILE)) {
                        reporter.error(new IOMessageImpl("Invalid file name {0}. File name may not match the name of the main project configuration.", null, -1, -1, name));
                    } else {
                        // write entry
                        zip.putNextEntry(new ZipEntry(name));
                        ProjectFile file = entry.getValue();
                        try {
                            LocatableOutputSupplier<OutputStream> target = new LocatableOutputSupplier<OutputStream>() {

                                private boolean first = true;

                                @Override
                                public OutputStream getOutput() throws IOException {
                                    if (first) {
                                        first = false;
                                        return new EntryOutputStream(zip);
                                    }
                                    throw new IllegalStateException("Output stream only available once");
                                }

                                @Override
                                public URI getLocation() {
                                    return getTarget().getLocation();
                                }
                            };
                            file.store(target);
                        } catch (Exception e) {
                            reporter.error(new IOMessageImpl("Error saving a project file.", e));
                            reporter.setSuccess(false);
                            return reporter;
                        }
                        zip.closeEntry();
                    }
                    progress.advance(1);
                }
            }
        } finally {
            zip.close();
        }
    } else {
        // save project file to XML
        OutputStream out = getTarget().getOutput();
        try {
            Project.save(getProject(), out);
        } catch (Exception e) {
            reporter.error(new IOMessageImpl("Could not save main project file.", e));
            reporter.setSuccess(false);
            return reporter;
        } finally {
            out.close();
        }
        progress.advance(1);
    }
    reporter.setSuccess(true);
    return reporter;
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) ProjectFile(eu.esdihumboldt.hale.common.core.io.project.model.ProjectFile) URI(java.net.URI) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException) LocatableOutputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.LocatableOutputSupplier) ZipOutputStream(java.util.zip.ZipOutputStream) ProjectFileInfo(eu.esdihumboldt.hale.common.core.io.project.model.ProjectFileInfo) FileOutputStream(java.io.FileOutputStream) ProjectFile(eu.esdihumboldt.hale.common.core.io.project.model.ProjectFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 37 with IOProviderConfigurationException

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

the class ProjectTransformationEnvironment method init.

/**
 * Initialize the environment based on the loaded project.
 *
 * @param project the project
 */
protected void init(Project project) {
    // export presets
    for (Entry<String, IOConfiguration> preset : project.getExportConfigurations().entrySet()) {
        IOConfiguration conf = preset.getValue();
        if (InstanceIO.ACTION_SAVE_TRANSFORMED_DATA.equals(conf.getActionId())) {
            // configuration for data export
            IOConfiguration c = conf.clone();
            // check provider
            IOProviderDescriptor factory = HaleIO.findIOProviderFactory(InstanceWriter.class, null, c.getProviderId());
            if (factory != null) {
                String name = preset.getKey();
                if (Strings.isNullOrEmpty(name)) {
                    name = factory.getDisplayName();
                }
                exportPresets.put(name, c);
            } else {
                log.error("I/O provider for export preset not found.");
            }
        }
    }
    // export templates
    Collection<IOProviderDescriptor> writerFactories = HaleIO.getProviderFactories(InstanceWriter.class);
    for (IOProviderDescriptor factory : writerFactories) {
        try {
            InstanceWriter writer = (InstanceWriter) factory.createExtensionObject();
            writer.setTargetSchema(getTargetSchema());
            writer.checkCompatibility();
            IOConfiguration conf = new IOConfiguration();
            conf.setActionId(InstanceIO.ACTION_SAVE_TRANSFORMED_DATA);
            conf.setProviderId(factory.getIdentifier());
            exportTemplates.put(factory.getDisplayName(), conf);
        } catch (IOProviderConfigurationException e) {
        // ignore
        } catch (Exception e) {
            log.error("Error initializing instance writer for testing compatibility", e);
        }
    }
}
Also used : IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOProviderDescriptor(eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor) InstanceWriter(eu.esdihumboldt.hale.common.instance.io.InstanceWriter) IOConfiguration(eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException)

Example 38 with IOProviderConfigurationException

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

the class CastorAlignmentReader method loadAlignment.

/**
 * @see AbstractIOProvider#execute(ProgressIndicator, IOReporter)
 */
@Override
protected MutableAlignment loadAlignment(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    progress.begin("Load hale alignment", ProgressIndicator.UNKNOWN);
    InputStream in = getSource().getInput();
    MutableAlignment alignment = null;
    try {
        alignment = CastorAlignmentIO.load(in, reporter, getSourceSchema(), getTargetSchema(), getPathUpdater());
    } catch (Exception e) {
        reporter.error(new IOMessageImpl(e.getMessage(), e));
        reporter.setSuccess(false);
        return alignment;
    } finally {
        in.close();
    }
    progress.end();
    reporter.setSuccess(true);
    return alignment;
}
Also used : InputStream(java.io.InputStream) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException)

Example 39 with IOProviderConfigurationException

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

the class CastorAlignmentWriter method execute.

/**
 * @see AbstractIOProvider#execute(ProgressIndicator, IOReporter)
 */
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    progress.begin("Save hale alignment", ProgressIndicator.UNKNOWN);
    PathUpdate pathUpdate = new PathUpdate(getProjectLocation(), getTarget().getLocation());
    OutputStream out = getTarget().getOutput();
    try {
        CastorAlignmentIO.save(getAlignment(), out, pathUpdate);
    } catch (Exception e) {
        reporter.error(new IOMessageImpl(e.getMessage(), e));
        reporter.setSuccess(false);
        return reporter;
    } finally {
        out.close();
    }
    progress.end();
    reporter.setSuccess(true);
    return reporter;
}
Also used : OutputStream(java.io.OutputStream) PathUpdate(eu.esdihumboldt.hale.common.core.io.PathUpdate) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException)

Example 40 with IOProviderConfigurationException

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

the class JaxbAlignmentWriter method execute.

/**
 * @see AbstractIOProvider#execute(ProgressIndicator, IOReporter)
 */
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    progress.begin("Save hale alignment", ProgressIndicator.UNKNOWN);
    PathUpdate pathUpdate = new PathUpdate(getProjectLocation(), getTarget().getLocation());
    AlignmentType alignment;
    try {
        alignment = JaxbAlignmentIO.convert(getAlignment(), reporter, pathUpdate);
    } catch (Exception e) {
        reporter.error(new IOMessageImpl("Error converting alignment to XML model", e));
        reporter.setSuccess(false);
        return reporter;
    }
    OutputStream out = getTarget().getOutput();
    try {
        JaxbAlignmentIO.save(alignment, reporter, out);
    } catch (Exception e) {
        reporter.error(new IOMessageImpl(e.getMessage(), e));
        reporter.setSuccess(false);
        return reporter;
    } finally {
        out.close();
    }
    progress.end();
    reporter.setSuccess(true);
    return reporter;
}
Also used : AlignmentType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AlignmentType) OutputStream(java.io.OutputStream) PathUpdate(eu.esdihumboldt.hale.common.core.io.PathUpdate) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException)

Aggregations

IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)44 IOException (java.io.IOException)38 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)27 InputStream (java.io.InputStream)14 URI (java.net.URI)13 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)7 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)6 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)6 DefaultSchema (eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchema)6 File (java.io.File)6 OutputStream (java.io.OutputStream)6 ArrayList (java.util.ArrayList)6 DefaultTypeDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)5 QName (javax.xml.namespace.QName)5 IOProviderDescriptor (eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor)4 DefaultPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition)4 InputStreamReader (java.io.InputStreamReader)4 Connection (java.sql.Connection)4 SQLException (java.sql.SQLException)4 PathUpdate (eu.esdihumboldt.hale.common.core.io.PathUpdate)3