From 9f47e4326f20b1ff04f9db01dfa2fe7fc90e60ea Mon Sep 17 00:00:00 2001 From: Alexander Nittka Date: Sun, 18 Mar 2018 13:50:15 +0100 Subject: [PATCH] find platform resources for file URI --- .../org/eclipse/emf/util/ResourceUtils.java | 39 +++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/org.eclipse.emf.util/src/org/eclipse/emf/util/ResourceUtils.java b/org.eclipse.emf.util/src/org/eclipse/emf/util/ResourceUtils.java index 6e554d3..402a93f 100644 --- a/org.eclipse.emf.util/src/org/eclipse/emf/util/ResourceUtils.java +++ b/org.eclipse.emf.util/src/org/eclipse/emf/util/ResourceUtils.java @@ -1,5 +1,9 @@ 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; @@ -23,9 +27,38 @@ public static IResource convertEResourceToPlatformResource(Resource eResource) { public static IResource findPlatformResource(URI uri) { if (uri.isPlatformResource()) { return ResourcesPlugin.getWorkspace().getRoot().findMember(uri.toPlatformString(true)); - } else { - return null; + } else if (uri.isFile()) { + java.net.URI fileURI = java.net.URI.create(uri.toString()); + List 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 findPlatformResources(java.net.URI fileURI) { + List 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; + } +} \ No newline at end of file