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