-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathResourceUtils.java
More file actions
64 lines (57 loc) · 1.85 KB
/
ResourceUtils.java
File metadata and controls
64 lines (57 loc) · 1.85 KB
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
package org.eclipse.emf.util;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
public class ResourceUtils {
private ResourceUtils() {
}
/**
* Returns the platform resource corresponding to the given EMF resource.
*/
public static IResource convertEResourceToPlatformResource(Resource eResource) {
return findPlatformResource(eResource.getURI());
}
/**
* Returns the platform resource with the given EMF URI.
*/
public static IResource findPlatformResource(URI uri) {
if (uri.isPlatformResource()) {
return ResourcesPlugin.getWorkspace().getRoot().findMember(uri.toPlatformString(true));
} else if (uri.isFile()) {
java.net.URI fileURI = java.net.URI.create(uri.toString());
List<IResource> candidates = findPlatformResources(fileURI);
if (!candidates.isEmpty()) {
return candidates.get(0);
}
}
return null;
}
/**
* returns a list of (existing) platform resource for the given file uri
*
* returns an empty list if no such resource exists or if the given URI is not a
* file uri
*/
public static List<IResource> findPlatformResources(java.net.URI fileURI) {
List<IResource> result = new ArrayList<>();
File file = new File(fileURI.getRawPath());// remove potential fragment
if (file.exists()) {
IResource[] candidates = null;
if (file.isDirectory()) {
candidates = ResourcesPlugin.getWorkspace().getRoot().findContainersForLocationURI(fileURI);
} else if (file.isFile()) {
candidates = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(fileURI);
}
for (IResource candidate : candidates) {
if (candidate.exists()) {
result.add(candidate);
}
}
}
return result;
}
}