use of eu.esdihumboldt.hale.common.core.io.project.model.ProjectFileInfo in project hale by halestudio.
the class ProjectToJaxb method convert.
/**
* Convert the given project to the corresponding JAXB type.
*
* @param project the project to convert
* @return the converted project
*/
public static ProjectType convert(Project project) {
ProjectType result = new ProjectType();
result.setAuthor(project.getAuthor());
result.setCreated(toXMLCalendar(project.getCreated()));
result.setDescription(project.getDescription());
result.setModified(toXMLCalendar(project.getModified()));
result.setName(project.getName());
result.setSaveConfig(toIOConfigurationType(project.getSaveConfiguration()));
if (project.getHaleVersion() != null) {
result.setVersion(project.getHaleVersion().toString());
}
// resources
for (IOConfiguration resource : project.getResources()) {
result.getResource().add(toIOConfigurationType(resource));
}
// export configs
for (Entry<String, IOConfiguration> confEntry : project.getExportConfigurations().entrySet()) {
IOConfigurationType conf = toIOConfigurationType(confEntry.getValue());
ExportConfigurationType exportConf = new ExportConfigurationType();
exportConf.setConfiguration(conf);
exportConf.setName(confEntry.getKey());
result.getExportConfig().add(exportConf);
}
// project files
for (ProjectFileInfo file : project.getProjectFiles()) {
result.getFile().add(toProjectFileType(file));
}
// properties
for (Entry<String, Value> property : project.getProperties().entrySet()) {
Object p = createProperty(property.getKey(), property.getValue());
if (p instanceof PropertyType) {
result.getAbstractProperty().add(of.createProperty((PropertyType) p));
} else if (p instanceof ComplexPropertyType) {
result.getAbstractProperty().add(of.createComplexProperty((ComplexPropertyType) p));
}
}
return result;
}
use of eu.esdihumboldt.hale.common.core.io.project.model.ProjectFileInfo 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());
}
}
use of eu.esdihumboldt.hale.common.core.io.project.model.ProjectFileInfo in project hale by halestudio.
the class ArchiveProjectWriter method createProjectArchive.
/**
* Creates the project archive.
*
* @param target {@link OutputStream} to write the archive to
* @param reporter the reporter to use for the execution report
* @param progress the progress indicator
* @return the execution report
* @throws IOException if an I/O operation fails
* @throws IOProviderConfigurationException if the I/O provider was not
* configured properly
*/
public IOReport createProjectArchive(OutputStream target, IOReporter reporter, ProgressIndicator progress) throws IOException, IOProviderConfigurationException {
ZipOutputStream zip = new ZipOutputStream(target);
// all files related to the project are copied into a temporary
// directory first and then packed into a zip file
// create temporary directory and project file
File tempDir = Files.createTempDir();
File baseFile = new File(tempDir, "project.halex");
// mark the temporary directory for clean-up if the project is closed
CleanupService clean = HalePlatform.getService(CleanupService.class);
if (clean != null) {
clean.addTemporaryFiles(CleanupContext.PROJECT, tempDir);
}
LocatableOutputSupplier<OutputStream> out = new FileIOSupplier(baseFile);
// false is correct if getParameter is null because false is default
boolean includeWebresources = getParameter(INCLUDE_WEB_RESOURCES).as(Boolean.class, false);
SubtaskProgressIndicator subtask = new SubtaskProgressIndicator(progress);
// save old IO configurations
List<IOConfiguration> oldResources = new ArrayList<IOConfiguration>();
for (int i = 0; i < getProject().getResources().size(); i++) {
// clone all IO configurations to work on different objects
oldResources.add(getProject().getResources().get(i).clone());
}
IOConfiguration config = getProject().getSaveConfiguration();
if (config == null) {
config = new IOConfiguration();
}
IOConfiguration oldSaveConfig = config.clone();
// copy resources to the temp directory and update xml schemas
updateResources(tempDir, includeWebresources, subtask, reporter);
// update target save configuration of the project
config.getProviderConfiguration().put(PARAM_TARGET, Value.of(baseFile.toURI().toString()));
// write project file via XMLProjectWriter
XMLProjectWriter writer = new XMLProjectWriter();
writer.setTarget(out);
writer.setProject(getProject());
writer.setProjectFiles(getProjectFiles());
IOReport report = writer.execute(progress, reporter);
// now after the project with its project files is written, look for the
// alignment file and update it
ProjectFileInfo newAlignmentInfo = getAlignmentFile(getProject());
if (newAlignmentInfo != null) {
URI newAlignment = tempDir.toURI().resolve(newAlignmentInfo.getLocation());
XMLAlignmentUpdater.update(new File(newAlignment), newAlignment, includeWebresources, reporter);
}
// put the complete temp directory into a zip file
IOUtils.zipDirectory(tempDir, zip);
zip.close();
// the files may not be deleted now as they will be needed if the
// project is saved again w/o loading it first
// update the relative resource locations
LocationUpdater updater = new LocationUpdater(getProject(), out.getLocation());
// resources are made absolute (else they can't be found afterwards),
// e.g. when saving the project again before loading it
updater.updateProject(false);
// reset the save configurations that has been overridden by the XML
// project writer
getProject().setSaveConfiguration(oldSaveConfig);
if (clean == null) {
// if no clean service is available, assume the directory is not
// needed anymore
FileUtils.deleteDirectory(tempDir);
}
return report;
}
use of eu.esdihumboldt.hale.common.core.io.project.model.ProjectFileInfo in project hale by halestudio.
the class DefaultProjectReader method execute.
/**
* @see AbstractIOProvider#execute(ProgressIndicator, IOReporter)
*/
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
progress.begin("Load project", ProgressIndicator.UNKNOWN);
InputStream in = getSource().getInput();
if (archive) {
// read from archive
ZipInputStream zip = new ZipInputStream(new BufferedInputStream(in));
try {
ZipEntry entry;
while ((entry = zip.getNextEntry()) != null) {
String name = entry.getName();
progress.setCurrentTask(MessageFormat.format("Load {0}", name));
if (name.equals(ProjectIO.PROJECT_FILE)) {
try {
setProjectChecked(Project.load(new EntryInputStream(zip)), reporter);
} catch (Exception e) {
// fail if main project file cannot be loaded
throw new IOProviderConfigurationException("Source is no valid project archive", e);
}
} else {
ProjectFile file = getProjectFiles().get(name);
if (file != null) {
try {
file.load(new EntryInputStream(zip));
} catch (Exception e) {
reporter.error(new IOMessageImpl("Error while loading project file {0}, file will be reset.", e, -1, -1, name));
// reset file
file.reset();
}
}
}
}
} finally {
zip.close();
}
} else {
// read from XML
try {
setProjectChecked(Project.load(in), reporter);
} catch (Exception e) {
// fail if main project file cannot be loaded
throw new IOProviderConfigurationException("Source is no valid project file", e);
} finally {
in.close();
}
}
URI oldProjectLocation;
if (getProject().getSaveConfiguration() == null) {
oldProjectLocation = getSource().getLocation();
} else {
oldProjectLocation = URI.create(getProject().getSaveConfiguration().getProviderConfiguration().get(ExportProvider.PARAM_TARGET).as(String.class));
}
PathUpdate update = new PathUpdate(oldProjectLocation, getSource().getLocation());
// check if there are any external project files listed
if (getProjectFiles() != null) {
// only if project files set at all
for (ProjectFileInfo fileInfo : getProject().getProjectFiles()) {
ProjectFile projectFile = getProjectFiles().get(fileInfo.getName());
if (projectFile != null) {
URI location = fileInfo.getLocation();
location = update.findLocation(location, false, DefaultInputSupplier.SCHEME_LOCAL.equals(getSource().getLocation().getScheme()), false);
if (location == null && getSource().getLocation() != null) {
// 1st try: appending file name to project location
try {
URI candidate = new URI(getSource().getLocation().toString() + "." + fileInfo.getName());
if (HaleIO.testStream(candidate, true)) {
location = candidate;
}
} catch (URISyntaxException e) {
// ignore
}
// 2nd try: file name next to project
if (location != null) {
try {
String projectLoc = getSource().getLocation().toString();
int index = projectLoc.lastIndexOf('/');
if (index > 0) {
URI candidate = new URI(projectLoc.substring(0, index + 1) + fileInfo.getName());
if (HaleIO.testStream(candidate, true)) {
location = candidate;
}
}
} catch (URISyntaxException e) {
// ignore
}
}
}
boolean fileSuccess = false;
if (location != null) {
try {
DefaultInputSupplier dis = new DefaultInputSupplier(location);
try (InputStream input = dis.getInput()) {
projectFile.load(input);
fileSuccess = true;
} catch (Exception e) {
// hand down
throw e;
}
} catch (Exception e) {
reporter.error(new IOMessageImpl("Loading project file failed", e));
}
}
if (!fileSuccess) {
reporter.error(new IOMessageImpl("Error while loading project file {0}, file will be reset.", null, -1, -1, fileInfo.getName()));
projectFile.reset();
}
} else {
reporter.info(new IOMessageImpl("No handler for external project file {0} found.", null, -1, -1, fileInfo.getName()));
}
}
}
// clear project infos
/*
* XXX was there any particular reason why this was done? I suspect it
* was done so when saving the project this information is not saved
* again as-is, but on the basis of actual files written. However, this
* case is handled in the project writer already.
*
* As this information is in fact necessary when trying to identify
* certain files like the alignment, clearing the list of project files
* was commented out.
*/
// getProject().getProjectFiles().clear();
progress.end();
reporter.setSuccess(true);
return reporter;
}
use of eu.esdihumboldt.hale.common.core.io.project.model.ProjectFileInfo 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;
}
Aggregations