Search in sources :

Example 6 with ProjectFile

use of eu.esdihumboldt.hale.common.core.io.project.model.ProjectFile 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;
}
Also used : DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) 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) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ZipInputStream(java.util.zip.ZipInputStream) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) BufferedInputStream(java.io.BufferedInputStream) ProjectFileInfo(eu.esdihumboldt.hale.common.core.io.project.model.ProjectFileInfo) PathUpdate(eu.esdihumboldt.hale.common.core.io.PathUpdate)

Example 7 with ProjectFile

use of eu.esdihumboldt.hale.common.core.io.project.model.ProjectFile 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 8 with ProjectFile

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

the class HeadlessProjectAdvisor method handleResults.

@Override
public void handleResults(ProjectReader provider) {
    project = provider.getProject();
    projectLocation = provider.getSource().getLocation();
    updater = new LocationUpdater(project, projectLocation);
    // no need to keep relative paths in the headless environment
    updater.updateProject(false);
    // inject project into advisors (mappable types)
    sourceSchemaAdvisor.setProject(project);
    targetSchemaAdvisor.setProject(project);
    // execute loaded I/O configurations
    List<IOConfiguration> confs = new ArrayList<IOConfiguration>(project.getResources());
    // but remove source data actions first
    Iterator<IOConfiguration> it = confs.iterator();
    while (it.hasNext()) {
        if (InstanceIO.ACTION_LOAD_SOURCE_DATA.equals(it.next().getActionId())) {
            it.remove();
        }
    }
    try {
        HeadlessIO.executeConfigurations(confs, advisors, reportHandler, this);
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    Map<String, ProjectFile> projectFiles = provider.getProjectFiles();
    // apply remaining project files
    for (ProjectFile file : projectFiles.values()) {
        file.apply();
    }
}
Also used : LocationUpdater(eu.esdihumboldt.hale.common.core.io.project.util.LocationUpdater) IOConfiguration(eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ProjectFile(eu.esdihumboldt.hale.common.core.io.project.model.ProjectFile) ActionProjectFile(eu.esdihumboldt.hale.common.core.io.project.extension.internal.ActionProjectFile)

Example 9 with ProjectFile

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

the class ArchiveProjectExportAdvisor method updateConfiguration.

@Override
public void updateConfiguration(ArchiveProjectWriter provider) {
    super.updateConfiguration(provider);
    Map<String, ProjectFile> files = ProjectIO.createDefaultProjectFiles(HaleUI.getServiceProvider());
    provider.setProjectFiles(files);
    URI projectLocation = getService(ProjectService.class).getLoadLocation();
    if (projectLocation != null) {
        provider.setPreviousTarget(projectLocation);
    }
}
Also used : ProjectService(eu.esdihumboldt.hale.ui.service.project.ProjectService) ProjectFile(eu.esdihumboldt.hale.common.core.io.project.model.ProjectFile) URI(java.net.URI)

Aggregations

ProjectFile (eu.esdihumboldt.hale.common.core.io.project.model.ProjectFile)9 File (java.io.File)4 URI (java.net.URI)4 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)3 IOException (java.io.IOException)3 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)2 IOConfiguration (eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration)2 ProjectFileInfo (eu.esdihumboldt.hale.common.core.io.project.model.ProjectFileInfo)2 ProjectService (eu.esdihumboldt.hale.ui.service.project.ProjectService)2 BufferedOutputStream (java.io.BufferedOutputStream)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 ZipEntry (java.util.zip.ZipEntry)2 PathUpdate (eu.esdihumboldt.hale.common.core.io.PathUpdate)1 IOProviderDescriptor (eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor)1 ProjectWriter (eu.esdihumboldt.hale.common.core.io.project.ProjectWriter)1 ProjectFileExtension (eu.esdihumboldt.hale.common.core.io.project.extension.ProjectFileExtension)1 ProjectFileFactory (eu.esdihumboldt.hale.common.core.io.project.extension.ProjectFileFactory)1 ActionProjectFile (eu.esdihumboldt.hale.common.core.io.project.extension.internal.ActionProjectFile)1 AdvisorProjectFile (eu.esdihumboldt.hale.common.core.io.project.model.AdvisorProjectFile)1