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
;
12 public class BasicSupportImages
{
14 * Check if the given resource can be a local image or a remote image, then
15 * refresh the cache with it if it is.
18 * the local directory to search, if any
20 * the resource to check
22 * @return the image if found, or NULL
25 static Image
getImage(BasicSupport support
, File dir
, String line
) {
26 URL url
= getImageUrl(support
, dir
, line
);
28 if ("file".equals(url
.getProtocol())) {
29 if (new File(url
.getPath()).isDirectory()) {
33 InputStream in
= null;
35 in
= Instance
.getCache().open(url
, support
, true);
37 } catch (IOException e
) {
42 } catch (IOException e
) {
52 * Check if the given resource can be a local image or a remote image, then
53 * refresh the cache with it if it is.
56 * the local directory to search, if any
58 * the resource to check
60 * @return the image URL if found, or NULL
63 static URL
getImageUrl(BasicSupport support
, File dir
, String line
) {
68 if (dir
!= null && dir
.exists() && !dir
.isFile()) {
71 String relPath
= null;
72 String absPath
= null;
74 relPath
= new File(dir
, line
.trim()).getAbsolutePath();
75 } catch (Exception e
) {
76 // Cannot be converted to path (one possibility to take
77 // into account: absolute path on Windows)
80 absPath
= new File(line
.trim()).getAbsolutePath();
81 } catch (Exception e
) {
82 // Cannot be converted to path (at all)
85 for (String ext
: getImageExt(true)) {
86 File absFile
= new File(absPath
+ ext
);
87 File relFile
= new File(relPath
+ ext
);
88 if (absPath
!= null && absFile
.exists()
89 && absFile
.isFile()) {
90 url
= absFile
.toURI().toURL();
91 } else if (relPath
!= null && relFile
.exists()
92 && relFile
.isFile()) {
93 url
= relFile
.toURI().toURL();
96 } catch (Exception e
) {
97 // Should not happen since we control the correct arguments
104 for (String ext
: getImageExt(true)) {
105 if (Instance
.getCache()
106 .check(new URL(line
+ ext
), true)) {
107 url
= new URL(line
+ ext
);
114 for (String ext
: getImageExt(true)) {
116 url
= new URL(line
+ ext
);
117 Instance
.getCache().refresh(url
, support
, true);
119 } catch (IOException e
) {
120 // no image with this ext
125 } catch (MalformedURLException e
) {
130 // refresh the cached file
133 Instance
.getCache().refresh(url
, support
, true);
134 } catch (IOException e
) {
135 // woops, broken image
145 * Return the list of supported image extensions.
147 * @param emptyAllowed
148 * TRUE to allow an empty extension on first place, which can be
149 * used when you may already have an extension in your input but
150 * are not sure about it
152 * @return the extensions
154 static String
[] getImageExt(boolean emptyAllowed
) {
156 return new String
[] { "", ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
159 return new String
[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" };