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
.getInstance().getCache().open(url
, support
, true);
58 Image img
= new Image(in
);
59 if (img
.getSize() == 0) {
61 throw new IOException(
62 "Empty image not accepted");
65 } catch (IOException e
) {
70 } catch (IOException e
) {
80 * Check if the given resource can be a local image or a remote image, then
81 * refresh the cache with it if it is.
84 * the support to use to download the resource (can be NULL)
86 * the local directory to search, if any
88 * the resource to check
90 * @return the image URL if found, or NULL
93 public URL
getImageUrl(BasicSupport support
, File dir
, String line
) {
98 if (dir
!= null && dir
.exists() && !dir
.isFile()) {
101 String relPath
= null;
102 String absPath
= null;
104 relPath
= new File(dir
, line
.trim()).getAbsolutePath();
105 } catch (Exception e
) {
106 // Cannot be converted to path (one possibility to take
107 // into account: absolute path on Windows)
110 absPath
= new File(line
.trim()).getAbsolutePath();
111 } catch (Exception e
) {
112 // Cannot be converted to path (at all)
115 for (String ext
: getImageExt(true)) {
116 File absFile
= new File(absPath
+ ext
);
117 File relFile
= new File(relPath
+ ext
);
118 if (absPath
!= null && absFile
.exists()
119 && absFile
.isFile()) {
120 url
= absFile
.toURI().toURL();
121 } else if (relPath
!= null && relFile
.exists()
122 && relFile
.isFile()) {
123 url
= relFile
.toURI().toURL();
126 } catch (Exception e
) {
127 // Should not happen since we control the correct arguments
134 for (String ext
: getImageExt(true)) {
135 if (Instance
.getInstance().getCache()
136 .check(new URL(line
+ ext
), true)) {
137 url
= new URL(line
+ ext
);
144 for (String ext
: getImageExt(true)) {
146 url
= new URL(line
+ ext
);
147 Instance
.getInstance().getCache().refresh(url
, support
, true);
149 } catch (IOException e
) {
150 // no image with this ext
155 } catch (MalformedURLException e
) {
160 // refresh the cached file
163 Instance
.getInstance().getCache().refresh(url
, support
, true);
164 } catch (IOException e
) {
165 // woops, broken image
175 * Return the list of supported image extensions.
177 * @param emptyAllowed
178 * TRUE to allow an empty extension on first place, which can be
179 * used when you may already have an extension in your input but
180 * are not sure about it
182 * @return the extensions
184 public String
[] getImageExt(boolean emptyAllowed
) {
186 return new String
[] { "", ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
189 return new String
[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" };