Skip to content

Commit 8db4eac

Browse files
author
pzi
committed
Restore correct URL/URI handling in URLImageDescriptor
The changes done with cee4631 cause an exception when URL is passed as an argument which contains improperly escaped characters (most noticeably whitespaces) and therefore causes an URISyntaxException when calling URL.toURI(). This does not occur when using IPath.fromOSString(), which is the approach that was done previously. But instead of converting this path back to an OS-dependent string, it is instead converted to a file, to check whether it exists and to only then convert it back to its String representation. A test case with an ill-formed URL has been added to avoid a similar problem in the future.
1 parent cee4631 commit 8db4eac

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

bundles/org.eclipse.jface/src/org/eclipse/jface/resource/URLImageDescriptor.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,18 @@
1919

2020

2121
import java.io.BufferedInputStream;
22+
import java.io.File;
2223
import java.io.IOException;
2324
import java.io.InputStream;
2425
import java.net.MalformedURLException;
25-
import java.net.URISyntaxException;
2626
import java.net.URL;
27-
import java.nio.file.Files;
28-
import java.nio.file.Path;
2927
import java.util.function.Function;
3028
import java.util.regex.Matcher;
3129
import java.util.regex.Pattern;
3230

3331
import org.eclipse.core.runtime.FileLocator;
3432
import org.eclipse.core.runtime.IAdaptable;
33+
import org.eclipse.core.runtime.IPath;
3534
import org.eclipse.core.runtime.Status;
3635
import org.eclipse.jface.internal.InternalPolicy;
3736
import org.eclipse.jface.util.Policy;
@@ -254,7 +253,7 @@ private static String getFilePath(URL url, boolean logException) {
254253
url = resolvePathVariables(url);
255254
URL locatedURL = FileLocator.toFileURL(url);
256255
return getFilePath(locatedURL);
257-
} catch (IOException | URISyntaxException e) {
256+
} catch (IOException e) {
258257
if (logException) {
259258
Policy.logException(e);
260259
} else if (InternalPolicy.DEBUG_LOG_URL_IMAGE_DESCRIPTOR_MISSING_2x) {
@@ -267,11 +266,11 @@ private static String getFilePath(URL url, boolean logException) {
267266
}
268267
}
269268

270-
private static String getFilePath(URL url) throws URISyntaxException {
269+
private static String getFilePath(URL url) {
271270
if (FILE_PROTOCOL.equalsIgnoreCase(url.getProtocol())) {
272-
Path filePath = Path.of(url.toURI());
273-
if (Files.exists(filePath)) {
274-
return filePath.toString();
271+
File file = IPath.fromOSString(url.getPath()).toFile();
272+
if (file.exists()) {
273+
return file.getPath();
275274
}
276275
}
277276
return null;

tests/org.eclipse.jface.tests/src/org/eclipse/jface/tests/images/UrlImageDescriptorTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.junit.Assert.assertNotSame;
2222
import static org.junit.Assert.assertNull;
2323

24+
import java.io.File;
2425
import java.io.IOException;
2526
import java.net.URL;
2627

@@ -154,6 +155,24 @@ private void testImageFileNameProviderGetxName_forFileURL(boolean osgiAvailable)
154155
}
155156
}
156157

158+
@Test
159+
public void testImageFileNameProviderGetxName_forFileURL_WhiteSpace() throws IOException {
160+
File imageFolder = tempFolder.newFolder("folder with spaces");
161+
File imageFile = new File(imageFolder, "image with spaces.png");
162+
imageFile.createNewFile();
163+
164+
// This is an invalid URL because the whitespace characters are not properly
165+
// encoded
166+
URL imageFileURL = new URL("file", null, imageFile.getPath());
167+
ImageDescriptor descriptor = ImageDescriptor.createFromURL(imageFileURL);
168+
169+
ImageFileNameProvider fileNameProvider = Adapters.adapt(descriptor, ImageFileNameProvider.class);
170+
assertNotNull("URLImageDescriptor does not adapt to ImageFileNameProvider", fileNameProvider);
171+
172+
String imagePath100 = fileNameProvider.getImagePath(100);
173+
assertNotNull("URLImageDescriptor ImageFileNameProvider does not return the 100% path", imagePath100);
174+
}
175+
157176
@Test
158177
public void testAdaptToURL() {
159178
ImageDescriptor descriptor = ImageDescriptor

0 commit comments

Comments
 (0)