Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b66b1cadeb2858909ebd09e6b9ab190fbcd7487d (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
95
96
97
98
99
100
101
/*****************************************************************************
 * Copyright (c) 2014 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:
 *  Patrick Tessier (CEA LIST) - Initial API and implementation
 /*****************************************************************************/
package org.eclipse.papyrus.infra.emf.utils;

import java.io.File;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.papyrus.infra.widgets.Activator;

//TODO
public class EMFFileUtil {

	/*
	 * Returns the path to the IFile (Encoded)
	 * 
	 * @param file
	 * @return
	 */
	public static String getPath(IFile file) {		
		URI uri = URI.createPlatformResourceURI(file.getFullPath().toString(), true);
		
		//ToString, toPlatformString
		return uri.toString();
	}

	/*
	 * Returns the IFile (Workspace file) from the given location.
	 * The location may be either absolute (From the FileSystem) or
	 * relative to the workspace root.
	 * 
	 * @param location
	 * @return
	 */
	public static IFile getIFile(String location) {
		URI uri = URI.createURI(location);
		IWorkspaceRoot workspace = ResourcesPlugin.getWorkspace().getRoot();
		
		//Search the file in the workspace
		if (uri.isPlatform()){
			Path workspacePath = new Path(uri.toPlatformString(true));
			return workspace.getFile(workspacePath);
		} else { //Then search it on the disk
			Path absolutePath = new Path(URI.decode(location));
			return workspace.getFileForLocation(absolutePath);
		}
	}

	/*
	 * Returns the Java File from the given location.
	 * The location may be either absolute (From the FileSystem) or
	 * relative to the workspace root.
	 * 
	 * @param location
	 * @return
	 */
	public static File getFile(String location) {
		IFile iFile = getIFile(location);
		if(iFile == null || !iFile.exists()) {
			return new File(location);
		}

		return new File(iFile.getLocationURI());
	}

	/*
	 * Returns the Java File from the given location.
	 * The location is relative to the workspace root.
	 * 
	 * @param location
	 * @return
	 */
	public static File getWorkspaceFile(String location) {
		IWorkspaceRoot workspace = ResourcesPlugin.getWorkspace().getRoot();
		IPath path = new Path(location);
		IFile currentFile = null;
		try {
			currentFile = workspace.getFile(path);
		} catch (IllegalArgumentException ex) {
			Activator.log.error(ex);
			return null;
		}

		return currentFile.getLocation().toFile();
	}
}

Back to the top