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 local directory to search, if any
26 * the resource to check
28 * @return the image if found, or NULL
31 public Image
getImage(BasicSupport support
, File dir
, String line
) {
32 URL url
= getImageUrl(support
, dir
, line
);
34 if ("file".equals(url
.getProtocol())) {
35 if (new File(url
.getPath()).isDirectory()) {
39 InputStream in
= null;
41 in
= Instance
.getCache().open(url
, support
, true);
43 } catch (IOException e
) {
48 } catch (IOException e
) {
58 * Check if the given resource can be a local image or a remote image, then
59 * refresh the cache with it if it is.
62 * the local directory to search, if any
64 * the resource to check
66 * @return the image URL if found, or NULL
69 public URL
getImageUrl(BasicSupport support
, File dir
, String line
) {
74 if (dir
!= null && dir
.exists() && !dir
.isFile()) {
77 String relPath
= null;
78 String absPath
= null;
80 relPath
= new File(dir
, line
.trim()).getAbsolutePath();
81 } catch (Exception e
) {
82 // Cannot be converted to path (one possibility to take
83 // into account: absolute path on Windows)
86 absPath
= new File(line
.trim()).getAbsolutePath();
87 } catch (Exception e
) {
88 // Cannot be converted to path (at all)
91 for (String ext
: getImageExt(true)) {
92 File absFile
= new File(absPath
+ ext
);
93 File relFile
= new File(relPath
+ ext
);
94 if (absPath
!= null && absFile
.exists()
95 && absFile
.isFile()) {
96 url
= absFile
.toURI().toURL();
97 } else if (relPath
!= null && relFile
.exists()
98 && relFile
.isFile()) {
99 url
= relFile
.toURI().toURL();
102 } catch (Exception e
) {
103 // Should not happen since we control the correct arguments
110 for (String ext
: getImageExt(true)) {
111 if (Instance
.getCache()
112 .check(new URL(line
+ ext
), true)) {
113 url
= new URL(line
+ ext
);
120 for (String ext
: getImageExt(true)) {
122 url
= new URL(line
+ ext
);
123 Instance
.getCache().refresh(url
, support
, true);
125 } catch (IOException e
) {
126 // no image with this ext
131 } catch (MalformedURLException e
) {
136 // refresh the cached file
139 Instance
.getCache().refresh(url
, support
, true);
140 } catch (IOException e
) {
141 // woops, broken image
151 * Return the list of supported image extensions.
153 * @param emptyAllowed
154 * TRUE to allow an empty extension on first place, which can be
155 * used when you may already have an extension in your input but
156 * are not sure about it
158 * @return the extensions
160 public String
[] getImageExt(boolean emptyAllowed
) {
162 return new String
[] { "", ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
165 return new String
[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" };