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 public 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 Image
getDefaultCover(String subject
) {
29 if (subject
!= null && !subject
.isEmpty() && Instance
.getInstance().getCoverDir() != null) {
31 File fileCover
= new File(Instance
.getInstance().getCoverDir(), subject
);
32 return getImage(null, fileCover
.toURI().toURL(), subject
);
33 } catch (MalformedURLException e
) {
41 * Return the list of supported image extensions.
44 * TRUE to allow an empty extension on first place, which can be
45 * used when you may already have an extension in your input but
46 * are not sure about it
48 * @return the extensions
50 public String
[] getImageExt(boolean emptyAllowed
) {
52 return new String
[] { "", ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
55 return new String
[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
59 * Check if the given resource can be a local image or a remote image, then
60 * refresh the cache with it if it is.
63 * the linked {@link BasicSupport} (can be NULL)
65 * the source of the story (for image lookup in the same path if
66 * the source is a file, can be NULL)
68 * the resource to check
70 * @return the image if found, or NULL
73 public 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
.getInstance().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} (can be NULL)
106 * the source of the story (for image lookup in the same path if
107 * the source is a file, can be NULL)
109 * the resource to check
111 * @return the image URL if found, or NULL
114 public URL
getImageUrl(BasicSupport support
, URL source
, String line
) {
119 if (source
!= null) {
122 String relPath
= null;
123 String absPath
= null;
125 String path
= new File(source
.getFile()).getParent();
126 relPath
= new File(new File(path
), line
.trim())
128 } catch (Exception e
) {
129 // Cannot be converted to path (one possibility to take
130 // into account: absolute path on Windows)
133 absPath
= new File(line
.trim()).getAbsolutePath();
134 } catch (Exception e
) {
135 // Cannot be converted to path (at all)
138 for (String ext
: getImageExt(true)) {
139 File absFile
= new File(absPath
+ ext
);
140 File relFile
= new File(relPath
+ ext
);
141 if (absPath
!= null && absFile
.exists()
142 && absFile
.isFile()) {
143 url
= absFile
.toURI().toURL();
144 } else if (relPath
!= null && relFile
.exists()
145 && relFile
.isFile()) {
146 url
= relFile
.toURI().toURL();
149 } catch (Exception e
) {
150 // Should not happen since we control the correct arguments
157 for (String ext
: getImageExt(true)) {
158 if (Instance
.getInstance().getCache().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
.getInstance().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
.getInstance().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 String
fixAuthor(String author
) {
205 if (author
!= null) {
206 for (String suffix
: new String
[] { " ", ":" }) {
207 for (String byString
: Instance
.getInstance().getConfig().getList(Config
.CONF_BYS
)) {
209 if (author
.toUpperCase().startsWith(byString
.toUpperCase())) {
210 author
= author
.substring(byString
.length()).trim();
215 // Special case (without suffix):
216 if (author
.startsWith("©")) {
217 author
= author
.substring(1);