Commit d86724f5 authored by Christoph Döbber's avatar Christoph Döbber
Browse files

finished import export of external references

refs 1007
parent 8986d291
......@@ -40,6 +40,7 @@ import org.fortiss.af3.project.AF3ProjectActivator;
import org.fortiss.af3.project.model.FileProject;
import org.fortiss.af3.project.model.impl.FileProjectImpl;
import org.fortiss.af3.project.storage.LocationProvider;
import org.fortiss.af3.project.utils.ExternalFileUtils;
/**
* Utility class providing import- and export-tools for file based projects.
......@@ -103,15 +104,18 @@ public class ExportProjectUtils {
*
* @param project
* the {@link FileProject} that should be exported
* @param path
* @param targetPath
* the {@link URI} location at which the project should be
* exported
*/
public static void exportProject(FileProject project, URI path) {
public static void exportProject(FileProject project, URI sourcePath, URI targetPath) {
try {
Resource r = new ResourceSetImpl().createResource(path);
Resource r = new ResourceSetImpl().createResource(targetPath);
r.getContents().add(project);
r.save(buildOptionsMap());
// Copy referenced external documents along accordingly.
ExternalFileUtils.copyExternalReferences(project, sourcePath, targetPath);
} catch(IOException e) {
error(AF3ProjectActivator.getDefault(), "File cannot be exported!", e);
e.printStackTrace();
......
......@@ -43,6 +43,7 @@ import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.fortiss.af3.project.model.FileProject;
import org.fortiss.af3.project.storage.LocationProvider;
import org.fortiss.af3.project.utils.ExternalFileUtils;
/**
* Utility methods for importing projects into workspace.
......@@ -156,6 +157,10 @@ public class ImportProjectUtils {
targetResource.getContents().add(sourceFileProject);
targetResource.save(buildOptionsMap());
// Copy referenced external documents along accordingly.
ExternalFileUtils.copyExternalReferences(sourceFileProject, sourceFileURI,
targetFileURI);
} catch(Exception e) {
error(getDefault(), "Error, cannot import AF3 project!", e);
openError(Display.getDefault().getActiveShell(), "Error, cannot import AF3 project!",
......
......@@ -21,13 +21,13 @@ import java.io.File;
import java.io.IOException;
import org.conqat.lib.commons.filesystem.FileSystemUtils;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.fortiss.af3.project.model.FileProject;
import org.fortiss.tooling.kernel.ToolingKernelActivator;
import org.fortiss.tooling.kernel.model.IExternalDocumentReference;
import org.fortiss.tooling.kernel.utils.LoggingUtils;
/**
*
......@@ -39,10 +39,10 @@ import org.fortiss.tooling.kernel.utils.LoggingUtils;
public class ExternalFileUtils {
/**
* Check given e-object and all its children for {@link IExternalDocumentReference}
*
* Copies along all external documents referenced relatively through
* {@link IExternalDocumentReference}
*/
public static void checkImportedFileReferences(FileProject fileProject, URI sourceFileURI,
public static void copyExternalReferences(FileProject fileProject, URI sourceFileURI,
URI targetFileURI) {
TreeIterator<EObject> currentContents = fileProject.eAllContents();
......@@ -52,19 +52,28 @@ public class ExternalFileUtils {
if(current instanceof IExternalDocumentReference) {
IExternalDocumentReference ref = (IExternalDocumentReference)current;
File sourcePath = new File(sourceFileURI.path()).getParentFile().getAbsoluteFile();
File sourcePath;
if(sourceFileURI.isPlatformResource()) {
IPath workspace = ResourcesPlugin.getWorkspace().getRoot().getLocation();
sourcePath =
new File(workspace.toFile(), sourceFileURI.toPlatformString(false))
.getParentFile();
} else {
sourcePath = new File(sourceFileURI.path()).getParentFile();
}
File destinationPath =
new File(targetFileURI.path()).getParentFile().getAbsoluteFile();
File sourceFile = new File(sourcePath, ref.getUri());
if(!sourceFile.exists()) {
LoggingUtils.warning(ToolingKernelActivator.getDefault(), "Could not load " +
ref.getUri());
// possibly hyperlink
continue;
}
// check file type
// copy
try {
FileSystemUtils.copyFile(sourceFile, new File(destinationPath, ref.getUri()));
} catch(IOException e) {
......
......@@ -301,7 +301,8 @@ public class ApplicationCommandHandler {
/**
* Handles the command action to create the Test Specific Example Action
*/
public static final class LoadSimpleTrafficLightsTestSpecificExampleCommandHandler extends AbstractHandler {
public static final class LoadSimpleTrafficLightsTestSpecificExampleCommandHandler extends
AbstractHandler {
/** {@inheritDoc} */
@Override
......@@ -500,7 +501,9 @@ public class ApplicationCommandHandler {
if(exportFileLocation != null) {
// Use copy to avoid error 'Cannot modify resource set
// without a write transaction'.
ExportProjectUtils.exportProject(EcoreUtil.copy(selectedProject),
selectedProject.eResource().getURI(),
URI.createFileURI(exportFileLocation));
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment