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
.fanfix
.bundles
.Config
;
11 import be
.nikiroo
.utils
.Image
;
14 * Helper class for {@link BasicSupport}, mostly dedicated to text formating for
15 * the classes that implement {@link BasicSupport}.
19 class BasicSupportHelper
{
21 * Get the default cover related to this subject (see <tt>.info</tt> files).
26 * @return the cover if any, or NULL
28 public static Image
getDefaultCover(String subject
) {
29 if (subject
!= null && !subject
.isEmpty()
30 && Instance
.getCoverDir() != null) {
32 File fileCover
= new File(Instance
.getCoverDir(), subject
);
33 return getImage(null, fileCover
.toURI().toURL(), subject
);
34 } catch (MalformedURLException e
) {
42 * Return the list of supported image extensions.
45 * TRUE to allow an empty extension on first place, which can be
46 * used when you may already have an extension in your input but
47 * are not sure about it
49 * @return the extensions
51 public static String
[] getImageExt(boolean emptyAllowed
) {
53 return new String
[] { "", ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
56 return new String
[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
60 * Check if the given resource can be a local image or a remote image, then
61 * refresh the cache with it if it is.
64 * the linked {@link BasicSupport}
68 * the resource to check
70 * @return the image if found, or NULL
73 public static Image
getImage(BasicSupport support
, URL source
, String line
) {
74 URL url
= getImageUrl(support
, source
, line
);
76 if ("file".equals(url
.getProtocol())) {
77 if (new File(url
.getPath()).isDirectory()) {
81 InputStream in
= null;
83 in
= Instance
.getCache().open(url
, support
, true);
85 } catch (IOException e
) {
90 } catch (IOException e
) {
100 * Check if the given resource can be a local image or a remote image, then
101 * refresh the cache with it if it is.
104 * the linked {@link BasicSupport}
108 * the resource to check
110 * @return the image URL if found, or NULL
113 public static URL
getImageUrl(BasicSupport support
, URL source
, String line
) {
118 if (source
!= null) {
121 String relPath
= null;
122 String absPath
= null;
124 String path
= new File(source
.getFile()).getParent();
125 relPath
= new File(new File(path
), line
.trim())
127 } catch (Exception e
) {
128 // Cannot be converted to path (one possibility to take
129 // into account: absolute path on Windows)
132 absPath
= new File(line
.trim()).getAbsolutePath();
133 } catch (Exception e
) {
134 // Cannot be converted to path (at all)
137 for (String ext
: getImageExt(true)) {
138 File absFile
= new File(absPath
+ ext
);
139 File relFile
= new File(relPath
+ ext
);
140 if (absPath
!= null && absFile
.exists()
141 && absFile
.isFile()) {
142 url
= absFile
.toURI().toURL();
143 } else if (relPath
!= null && relFile
.exists()
144 && relFile
.isFile()) {
145 url
= relFile
.toURI().toURL();
148 } catch (Exception e
) {
149 // Should not happen since we control the correct arguments
156 for (String ext
: getImageExt(true)) {
157 if (Instance
.getCache()
158 .check(new URL(line
+ ext
), true)) {
159 url
= new URL(line
+ ext
);
166 for (String ext
: getImageExt(true)) {
168 url
= new URL(line
+ ext
);
169 Instance
.getCache().refresh(url
, support
, true);
171 } catch (IOException e
) {
172 // no image with this ext
177 } catch (MalformedURLException e
) {
182 // refresh the cached file
185 Instance
.getCache().refresh(url
, support
, true);
186 } catch (IOException e
) {
187 // woops, broken image
197 * Fix the author name if it is prefixed with some "by" {@link String}.
200 * the author with a possible prefix
202 * @return the author without prefixes
204 public static String
fixAuthor(String author
) {
205 if (author
!= null) {
206 for (String suffix
: new String
[] { " ", ":" }) {
207 for (String byString
: Instance
.getConfig()
208 .getString(Config
.BYS
).split(",")) {
210 if (author
.toUpperCase().startsWith(byString
.toUpperCase())) {
211 author
= author
.substring(byString
.length()).trim();
216 // Special case (without suffix):
217 if (author
.startsWith("©")) {
218 author
= author
.substring(1);