1 package be
.nikiroo
.fanfix
.supported
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
6 import java
.net
.MalformedURLException
;
9 import be
.nikiroo
.fanfix
.Instance
;
10 import be
.nikiroo
.utils
.Image
;
13 * Helper class for {@link BasicSupport}, mostly dedicated to images for
14 * the classes that implement {@link BasicSupport}.
18 public class BasicSupportImages
{
20 * Check if the given resource can be a local image or a remote image, then
21 * refresh the cache with it if it is.
24 * the support to use to download the resource (can be NULL)
26 * the local directory to search, if any
28 * the resource to check
30 * @return the image if found, or NULL
32 public Image
getImage(BasicSupport support
, File dir
, String line
) {
33 URL url
= getImageUrl(support
, dir
, line
);
34 return getImage(support
,url
);
38 * Check if the given resource can be a local image or a remote image, then
39 * refresh the cache with it if it is.
42 * the support to use to download the resource (can be NULL)
44 * the actual URL to check (file or remote, can be NULL)
46 * @return the image if found, or NULL
48 public Image
getImage(BasicSupport support
, URL url
) {
50 if ("file".equals(url
.getProtocol())) {
51 if (new File(url
.getPath()).isDirectory()) {
55 InputStream in
= null;
57 in
= Instance
.getCache().open(url
, support
, true);
59 } catch (IOException e
) {
64 } catch (IOException e
) {
74 * Check if the given resource can be a local image or a remote image, then
75 * refresh the cache with it if it is.
78 * the support to use to download the resource (can be NULL)
80 * the local directory to search, if any
82 * the resource to check
84 * @return the image URL if found, or NULL
87 public URL
getImageUrl(BasicSupport support
, File dir
, String line
) {
92 if (dir
!= null && dir
.exists() && !dir
.isFile()) {
95 String relPath
= null;
96 String absPath
= null;
98 relPath
= new File(dir
, line
.trim()).getAbsolutePath();
99 } catch (Exception e
) {
100 // Cannot be converted to path (one possibility to take
101 // into account: absolute path on Windows)
104 absPath
= new File(line
.trim()).getAbsolutePath();
105 } catch (Exception e
) {
106 // Cannot be converted to path (at all)
109 for (String ext
: getImageExt(true)) {
110 File absFile
= new File(absPath
+ ext
);
111 File relFile
= new File(relPath
+ ext
);
112 if (absPath
!= null && absFile
.exists()
113 && absFile
.isFile()) {
114 url
= absFile
.toURI().toURL();
115 } else if (relPath
!= null && relFile
.exists()
116 && relFile
.isFile()) {
117 url
= relFile
.toURI().toURL();
120 } catch (Exception e
) {
121 // Should not happen since we control the correct arguments
128 for (String ext
: getImageExt(true)) {
129 if (Instance
.getCache()
130 .check(new URL(line
+ ext
), true)) {
131 url
= new URL(line
+ ext
);
138 for (String ext
: getImageExt(true)) {
140 url
= new URL(line
+ ext
);
141 Instance
.getCache().refresh(url
, support
, true);
143 } catch (IOException e
) {
144 // no image with this ext
149 } catch (MalformedURLException e
) {
154 // refresh the cached file
157 Instance
.getCache().refresh(url
, support
, true);
158 } catch (IOException e
) {
159 // woops, broken image
169 * Return the list of supported image extensions.
171 * @param emptyAllowed
172 * TRUE to allow an empty extension on first place, which can be
173 * used when you may already have an extension in your input but
174 * are not sure about it
176 * @return the extensions
178 public String
[] getImageExt(boolean emptyAllowed
) {
180 return new String
[] { "", ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
183 return new String
[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" };