Skip to content

Simplify and clean-up URLImageDescriptor #2938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.function.Function;
import java.util.function.Supplier;

import org.eclipse.core.runtime.FileLocator;
Expand Down Expand Up @@ -52,55 +53,24 @@
*/
class URLImageDescriptor extends ImageDescriptor implements IAdaptable {

private static class URLImageFileNameProvider implements ImageFileNameProvider {

private final String url;

public URLImageFileNameProvider(String url) {
this.url = url;
}

@Override
public String getImagePath(int zoom) {
private ImageFileNameProvider createURLImageFileNameProvider() {
return zoom -> {
URL tempURL = getURL(url);
if (tempURL != null) {
final boolean logIOException = zoom == 100;
if (zoom == 100) {
// Do not take this path if the image file can be scaled up dynamically.
// The calling image will do that itself!
return getFilePath(tempURL, logIOException);
}
URL xUrl = getxURL(tempURL, zoom);
if (xUrl != null) {
String xResult = getFilePath(xUrl, logIOException);
if (xResult != null) {
return xResult;
}
}
String xpath = FileImageDescriptor.getxPath(url, zoom);
if (xpath != null) {
URL xPathUrl = getURL(xpath);
if (xPathUrl != null) {
return getFilePath(xPathUrl, logIOException);
}
}
return getZoomedImageSource(tempURL, url, zoom, u -> getFilePath(u, logIOException));
}
return null;
}

};
}

private static class URLImageDataProvider implements ImageDataProvider {

private final String url;

public URLImageDataProvider(String url) {
this.url = url;
}

@Override
public ImageData getImageData(int zoom) {
return URLImageDescriptor.getImageData(url, zoom);
}

private ImageDataProvider createURLImageDataProvider() {
return zoom -> getImageData(zoom);
}

private static long cumulativeTime;
Expand All @@ -124,36 +94,37 @@ public ImageData getImageData(int zoom) {

@Override
public boolean equals(Object o) {
if (!(o instanceof URLImageDescriptor)) {
if (!(o instanceof URLImageDescriptor other)) {
return false;
}
return ((URLImageDescriptor) o).url.equals(this.url);
return other.url.equals(this.url);
}

@Override
public ImageData getImageData(int zoom) {
return getImageData(url, zoom);
}

private static ImageData getImageData(String url, int zoom) {
URL tempURL = getURL(url);
if (tempURL != null) {
if (zoom == 100 || canLoadAtZoom(() -> getStream(tempURL), zoom)) {
return getImageData(tempURL, 100, zoom);
}
URL xUrl = getxURL(tempURL, zoom);
if (xUrl != null) {
ImageData xdata = getImageData(xUrl, zoom, zoom);
if (xdata != null) {
return xdata;
}
return getZoomedImageSource(tempURL, url, zoom, u -> getImageData(u, zoom, zoom));
}
return null;
}

private static <R> R getZoomedImageSource(URL url, String urlString, int zoom, Function<URL, R> getImage) {
URL xUrl = getxURL(url, zoom);
if (xUrl != null) {
R xdata = getImage.apply(xUrl);
if (xdata != null) {
return xdata;
}
String xpath = FileImageDescriptor.getxPath(url, zoom);
if (xpath != null) {
URL xPathUrl = getURL(xpath);
if (xPathUrl != null) {
return getImageData(xPathUrl, zoom, zoom);
}
}
String xpath = FileImageDescriptor.getxPath(urlString, zoom);
if (xpath != null) {
URL xPathUrl = getURL(xpath);
if (xPathUrl != null) {
return getImage.apply(xPathUrl);
}
}
return null;
Expand All @@ -165,7 +136,6 @@ private static ImageData getImageData(URL url, int fileZoom, int targetZoom) {
}

static ImageData loadImageData(InputStream stream, int fileZoom, int targetZoom) {
ImageData result = null;
try (InputStream in = stream) {
if (in != null) {
return loadImageFromStream(new BufferedInputStream(in), fileZoom, targetZoom);
Expand All @@ -176,9 +146,9 @@ static ImageData loadImageData(InputStream stream, int fileZoom, int targetZoom)
// fall through otherwise
}
} catch (IOException e) {
Policy.getLog().log(Status.error(e.getLocalizedMessage(), e));
Policy.logException(e);
}
return result;
return null;
}

@SuppressWarnings("restriction")
Expand All @@ -194,32 +164,15 @@ static boolean canLoadAtZoom(Supplier<InputStream> stream, int zoom) {
return FileFormat.canLoadAtZoom(new ElementAtZoom<>(in, 100), zoom);
}
} catch (IOException e) {
Policy.getLog().log(Status.error(e.getLocalizedMessage(), e));
Policy.logException(e);
}
return false;
}

/**
* Returns a stream on the image contents. Returns null if a stream could
* not be opened.
*
* @return the stream for loading the data
*/
protected InputStream getStream() {
return getStream(getURL(url));
}

private static InputStream getStream(URL url) {
if (url == null) {
return null;
}

try {
if (InternalPolicy.OSGI_AVAILABLE) {
URL platformURL = FileLocator.find(url);
if (platformURL != null) {
url = platformURL;
}
url = resolvePathVariables(url);
}
return url.openStream();
} catch (IOException e) {
Expand Down Expand Up @@ -265,7 +218,7 @@ private static URL getxURL(URL url, int zoom) {
}
return new URL(url.getProtocol(), url.getHost(), url.getPort(), file);
} catch (MalformedURLException e) {
Policy.getLog().log(Status.error(e.getLocalizedMessage(), e));
Policy.logException(e);
}
}
return null;
Expand All @@ -284,11 +237,7 @@ private static String getFilePath(URL url, boolean logIOException) {
return IPath.fromOSString(url.getFile()).toOSString();
return null;
}

URL platformURL = FileLocator.find(url);
if (platformURL != null) {
url = platformURL;
}
url = resolvePathVariables(url);
URL locatedURL = FileLocator.toFileURL(url);
if (FILE_PROTOCOL.equalsIgnoreCase(locatedURL.getProtocol())) {
String filePath = IPath.fromOSString(locatedURL.getPath()).toOSString();
Expand All @@ -310,6 +259,14 @@ private static String getFilePath(URL url, boolean logIOException) {
}
}

private static URL resolvePathVariables(URL url) {
URL platformURL = FileLocator.find(url); // Resolve variables within URL's path
if (platformURL != null) {
url = platformURL;
}
return url;
}

@Override
public Image createImage(boolean returnMissingImageOnError, Device device) {
long start = 0;
Expand All @@ -323,7 +280,7 @@ public Image createImage(boolean returnMissingImageOnError, Device device) {
// We really want a fresh ImageFileNameProvider instance to make
// sure the code that uses created images can use equals(),
// see Image#equals
return new Image(device, new URLImageFileNameProvider(url));
return new Image(device, createURLImageFileNameProvider());
} catch (SWTException | IllegalArgumentException exception) {
// If we fail fall back to the slower input stream method.
}
Expand All @@ -334,7 +291,7 @@ public Image createImage(boolean returnMissingImageOnError, Device device) {
// We really want a fresh ImageDataProvider instance to make
// sure the code that uses created images can use equals(),
// see Image#equals
image = new Image(device, new URLImageDataProvider(url));
image = new Image(device, createURLImageDataProvider());
} catch (SWTException e) {
if (e.code != SWT.ERROR_INVALID_IMAGE) {
throw e;
Expand Down Expand Up @@ -383,7 +340,7 @@ private static URL getURL(String urlString) {
try {
result = new URL(urlString);
} catch (MalformedURLException e) {
Policy.getLog().log(Status.error(e.getLocalizedMessage(), e));
Policy.logException(e);
}
return result;
}
Expand All @@ -394,10 +351,10 @@ public <T> T getAdapter(Class<T> adapter) {
return adapter.cast(getURL(url));
}
if (adapter == ImageFileNameProvider.class) {
return adapter.cast(new URLImageFileNameProvider(url));
return adapter.cast(createURLImageFileNameProvider());
}
if (adapter == ImageDataProvider.class) {
return adapter.cast(new URLImageDataProvider(url));
return adapter.cast(createURLImageDataProvider());
}
return null;
}
Expand Down
Loading