Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a3d2fc27bd889bf0ed2bc1a6924062d002df9854 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*****************************************************************************
 * Copyright (c) 2013 CEA LIST.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   CEA LIST - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.cdo.internal.core.exporter;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.DiagnosticChain;
import org.eclipse.emf.common.util.URI;
import org.eclipse.osgi.util.NLS;
import org.eclipse.papyrus.cdo.core.exporter.IModelExportMapping;
import org.eclipse.papyrus.cdo.core.importer.IModelTransferConfiguration;
import org.eclipse.papyrus.cdo.core.importer.IModelTransferNode;
import org.eclipse.papyrus.cdo.internal.core.Activator;
import org.eclipse.papyrus.cdo.internal.core.importer.AbstractModelTransferMapping;
import org.eclipse.papyrus.cdo.internal.core.l10n.Messages;

/**
 * This is the AbstractModelImportMapping type. Enjoy.
 */
public class ModelExportMapping extends AbstractModelTransferMapping implements IModelExportMapping {

	private final IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot();

	public ModelExportMapping(IModelTransferConfiguration config) {
		super(config);
	}

	@Override
	protected boolean validateMapping(IModelTransferNode node, DiagnosticChain diagnostics) {
		boolean result = true;

		IPath mapping = getMapping(node);
		if(mapping == null) {
			diagnostics.add(new BasicDiagnostic(Diagnostic.ERROR, Activator.PLUGIN_ID, 0, NLS.bind(Messages.ModelExportMapping_0, node.getName()), new Object[]{ node }));
			result = false;
		} else {
			for(URI next : node.getResourceURIs()) {
				IPath resourcePath = mapping.removeFileExtension().addFileExtension(next.fileExtension());
				IPath containerPath = mapping.removeLastSegments(1);

				IContainer container = (containerPath.segmentCount() == 1) ? wsRoot.getProject(containerPath.segment(0)) : wsRoot.getFolder(containerPath);
				if(!container.getProject().exists()) {
					diagnostics.add(new BasicDiagnostic(Diagnostic.ERROR, Activator.PLUGIN_ID, 0, NLS.bind(Messages.ModelExportMapping_1, mapping, node.getName()), new Object[]{ node }));
					result = false;
					break;
				} else if(!container.exists()) {
					// does it not exist because there is a file of that name?
					IFile file = wsRoot.getFile(containerPath);
					if(file.exists()) {
						diagnostics.add(new BasicDiagnostic(Diagnostic.ERROR, Activator.PLUGIN_ID, 0, NLS.bind(Messages.ModelExportMapping_2, containerPath, node.getName()), new Object[]{ node }));
						result = false;
						break;
					}
				} else {
					// the container exists.  Does the mapped file clash?
					IFile file = wsRoot.getFile(resourcePath);
					if(file.exists()) {
						diagnostics.add(new BasicDiagnostic(Diagnostic.ERROR, Activator.PLUGIN_ID, 0, NLS.bind(Messages.ModelExportMapping_3, resourcePath, node.getName()), new Object[]{ node }));
						result = false;
						break;
					} else {
						// final check is whether the workspace will allow creation of this resource
						IStatus status = wsRoot.getWorkspace().validatePath(resourcePath.toString(), IResource.FILE);
						if(!status.isOK()) {
							diagnostics.add(BasicDiagnostic.toDiagnostic(status));
							if(status.getSeverity() > IStatus.WARNING) {
								result = false;
								break;
							}
						}
					}
				}
			}
		}

		return result;
	}
}

Back to the top