2525import java .net .URL ;
2626import java .nio .file .Files ;
2727import java .nio .file .Path ;
28+ import java .util .function .Function ;
2829import java .util .function .Supplier ;
2930
3031import org .eclipse .core .runtime .FileLocator ;
5253 */
5354class URLImageDescriptor extends ImageDescriptor implements IAdaptable {
5455
55- private static class URLImageFileNameProvider implements ImageFileNameProvider {
56-
57- private final String url ;
58-
59- public URLImageFileNameProvider (String url ) {
60- this .url = url ;
61- }
62-
63- @ Override
64- public String getImagePath (int zoom ) {
56+ private ImageFileNameProvider createURLImageFileNameProvider () {
57+ return zoom -> {
6558 URL tempURL = getURL (url );
6659 if (tempURL != null ) {
6760 final boolean logIOException = zoom == 100 ;
6861 if (zoom == 100 ) {
62+ // Do not take this path if the image file can be scaled up dynamically.
63+ // The calling image will do that itself!
6964 return getFilePath (tempURL , logIOException );
7065 }
71- URL xUrl = getxURL (tempURL , zoom );
72- if (xUrl != null ) {
73- String xResult = getFilePath (xUrl , logIOException );
74- if (xResult != null ) {
75- return xResult ;
76- }
77- }
78- String xpath = FileImageDescriptor .getxPath (url , zoom );
79- if (xpath != null ) {
80- URL xPathUrl = getURL (xpath );
81- if (xPathUrl != null ) {
82- return getFilePath (xPathUrl , logIOException );
83- }
84- }
66+ return getZoomedImageSource (tempURL , url , zoom , u -> getFilePath (u , logIOException ));
8567 }
8668 return null ;
87- }
88-
69+ };
8970 }
9071
91- private static class URLImageDataProvider implements ImageDataProvider {
92-
93- private final String url ;
94-
95- public URLImageDataProvider (String url ) {
96- this .url = url ;
97- }
98-
99- @ Override
100- public ImageData getImageData (int zoom ) {
101- return URLImageDescriptor .getImageData (url , zoom );
102- }
103-
72+ private ImageDataProvider createURLImageDataProvider () {
73+ return zoom -> getImageData (zoom );
10474 }
10575
10676 private static long cumulativeTime ;
@@ -124,36 +94,37 @@ public ImageData getImageData(int zoom) {
12494
12595 @ Override
12696 public boolean equals (Object o ) {
127- if (!(o instanceof URLImageDescriptor )) {
97+ if (!(o instanceof URLImageDescriptor other )) {
12898 return false ;
12999 }
130- return (( URLImageDescriptor ) o ) .url .equals (this .url );
100+ return other .url .equals (this .url );
131101 }
132102
133103 @ Override
134104 public ImageData getImageData (int zoom ) {
135- return getImageData (url , zoom );
136- }
137-
138- private static ImageData getImageData (String url , int zoom ) {
139105 URL tempURL = getURL (url );
140106 if (tempURL != null ) {
141107 if (zoom == 100 || canLoadAtZoom (() -> getStream (tempURL ), zoom )) {
142108 return getImageData (tempURL , 100 , zoom );
143109 }
144- URL xUrl = getxURL (tempURL , zoom );
145- if (xUrl != null ) {
146- ImageData xdata = getImageData (xUrl , zoom , zoom );
147- if (xdata != null ) {
148- return xdata ;
149- }
110+ return getZoomedImageSource (tempURL , url , zoom , u -> getImageData (u , zoom , zoom ));
111+ }
112+ return null ;
113+ }
114+
115+ private static <R > R getZoomedImageSource (URL url , String urlString , int zoom , Function <URL , R > getImage ) {
116+ URL xUrl = getxURL (url , zoom );
117+ if (xUrl != null ) {
118+ R xdata = getImage .apply (xUrl );
119+ if (xdata != null ) {
120+ return xdata ;
150121 }
151- String xpath = FileImageDescriptor . getxPath ( url , zoom );
152- if ( xpath != null ) {
153- URL xPathUrl = getURL ( xpath );
154- if ( xPathUrl != null ) {
155- return getImageData (xPathUrl , zoom , zoom );
156- }
122+ }
123+ String xpath = FileImageDescriptor . getxPath ( urlString , zoom );
124+ if ( xpath != null ) {
125+ URL xPathUrl = getURL ( xpath );
126+ if (xPathUrl != null ) {
127+ return getImage . apply ( xPathUrl );
157128 }
158129 }
159130 return null ;
@@ -165,7 +136,6 @@ private static ImageData getImageData(URL url, int fileZoom, int targetZoom) {
165136 }
166137
167138 static ImageData loadImageData (InputStream stream , int fileZoom , int targetZoom ) {
168- ImageData result = null ;
169139 try (InputStream in = stream ) {
170140 if (in != null ) {
171141 return loadImageFromStream (new BufferedInputStream (in ), fileZoom , targetZoom );
@@ -176,9 +146,9 @@ static ImageData loadImageData(InputStream stream, int fileZoom, int targetZoom)
176146 // fall through otherwise
177147 }
178148 } catch (IOException e ) {
179- Policy .getLog (). log ( Status . error ( e . getLocalizedMessage (), e ) );
149+ Policy .logException ( e );
180150 }
181- return result ;
151+ return null ;
182152 }
183153
184154 @ SuppressWarnings ("restriction" )
@@ -194,32 +164,15 @@ static boolean canLoadAtZoom(Supplier<InputStream> stream, int zoom) {
194164 return FileFormat .canLoadAtZoom (new ElementAtZoom <>(in , 100 ), zoom );
195165 }
196166 } catch (IOException e ) {
197- Policy .getLog (). log ( Status . error ( e . getLocalizedMessage (), e ) );
167+ Policy .logException ( e );
198168 }
199169 return false ;
200170 }
201171
202- /**
203- * Returns a stream on the image contents. Returns null if a stream could
204- * not be opened.
205- *
206- * @return the stream for loading the data
207- */
208- protected InputStream getStream () {
209- return getStream (getURL (url ));
210- }
211-
212172 private static InputStream getStream (URL url ) {
213- if (url == null ) {
214- return null ;
215- }
216-
217173 try {
218174 if (InternalPolicy .OSGI_AVAILABLE ) {
219- URL platformURL = FileLocator .find (url );
220- if (platformURL != null ) {
221- url = platformURL ;
222- }
175+ url = resolvePathVariables (url );
223176 }
224177 return url .openStream ();
225178 } catch (IOException e ) {
@@ -265,7 +218,7 @@ private static URL getxURL(URL url, int zoom) {
265218 }
266219 return new URL (url .getProtocol (), url .getHost (), url .getPort (), file );
267220 } catch (MalformedURLException e ) {
268- Policy .getLog (). log ( Status . error ( e . getLocalizedMessage (), e ) );
221+ Policy .logException ( e );
269222 }
270223 }
271224 return null ;
@@ -284,11 +237,7 @@ private static String getFilePath(URL url, boolean logIOException) {
284237 return IPath .fromOSString (url .getFile ()).toOSString ();
285238 return null ;
286239 }
287-
288- URL platformURL = FileLocator .find (url );
289- if (platformURL != null ) {
290- url = platformURL ;
291- }
240+ url = resolvePathVariables (url );
292241 URL locatedURL = FileLocator .toFileURL (url );
293242 if (FILE_PROTOCOL .equalsIgnoreCase (locatedURL .getProtocol ())) {
294243 String filePath = IPath .fromOSString (locatedURL .getPath ()).toOSString ();
@@ -310,6 +259,14 @@ private static String getFilePath(URL url, boolean logIOException) {
310259 }
311260 }
312261
262+ private static URL resolvePathVariables (URL url ) {
263+ URL platformURL = FileLocator .find (url ); // Resolve variables within URL's path
264+ if (platformURL != null ) {
265+ url = platformURL ;
266+ }
267+ return url ;
268+ }
269+
313270 @ Override
314271 public Image createImage (boolean returnMissingImageOnError , Device device ) {
315272 long start = 0 ;
@@ -323,7 +280,7 @@ public Image createImage(boolean returnMissingImageOnError, Device device) {
323280 // We really want a fresh ImageFileNameProvider instance to make
324281 // sure the code that uses created images can use equals(),
325282 // see Image#equals
326- return new Image (device , new URLImageFileNameProvider ( url ));
283+ return new Image (device , createURLImageFileNameProvider ( ));
327284 } catch (SWTException | IllegalArgumentException exception ) {
328285 // If we fail fall back to the slower input stream method.
329286 }
@@ -334,7 +291,7 @@ public Image createImage(boolean returnMissingImageOnError, Device device) {
334291 // We really want a fresh ImageDataProvider instance to make
335292 // sure the code that uses created images can use equals(),
336293 // see Image#equals
337- image = new Image (device , new URLImageDataProvider ( url ));
294+ image = new Image (device , createURLImageDataProvider ( ));
338295 } catch (SWTException e ) {
339296 if (e .code != SWT .ERROR_INVALID_IMAGE ) {
340297 throw e ;
@@ -383,7 +340,7 @@ private static URL getURL(String urlString) {
383340 try {
384341 result = new URL (urlString );
385342 } catch (MalformedURLException e ) {
386- Policy .getLog (). log ( Status . error ( e . getLocalizedMessage (), e ) );
343+ Policy .logException ( e );
387344 }
388345 return result ;
389346 }
@@ -394,10 +351,10 @@ public <T> T getAdapter(Class<T> adapter) {
394351 return adapter .cast (getURL (url ));
395352 }
396353 if (adapter == ImageFileNameProvider .class ) {
397- return adapter .cast (new URLImageFileNameProvider ( url ));
354+ return adapter .cast (createURLImageFileNameProvider ( ));
398355 }
399356 if (adapter == ImageDataProvider .class ) {
400- return adapter .cast (new URLImageDataProvider ( url ));
357+ return adapter .cast (createURLImageDataProvider ( ));
401358 }
402359 return null ;
403360 }
0 commit comments