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()
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 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} (can be NULL)
66 * the source of the story (for image lookup in the same path if
67 * the source is a file, can be NULL)
69 * the resource to check
71 * @return the image if found, or NULL
74 public Image
getImage(BasicSupport support
, URL source
, String line
) {
75 URL url
= getImageUrl(support
, source
, line
);
77 if ("file".equals(url
.getProtocol())) {
78 if (new File(url
.getPath()).isDirectory()) {
82 InputStream in
= null;
84 in
= Instance
.getCache().open(url
, support
, true);
86 } catch (IOException e
) {
91 } catch (IOException e
) {
101 * Check if the given resource can be a local image or a remote image, then
102 * refresh the cache with it if it is.
105 * the linked {@link BasicSupport} (can be NULL)
107 * the source of the story (for image lookup in the same path if
108 * the source is a file, can be NULL)
110 * the resource to check
112 * @return the image URL if found, or NULL
115 public URL
getImageUrl(BasicSupport support
, URL source
, String line
) {
120 if (source
!= null) {
123 String relPath
= null;
124 String absPath
= null;
126 String path
= new File(source
.getFile()).getParent();
127 relPath
= new File(new File(path
), line
.trim())
129 } catch (Exception e
) {
130 // Cannot be converted to path (one possibility to take
131 // into account: absolute path on Windows)
134 absPath
= new File(line
.trim()).getAbsolutePath();
135 } catch (Exception e
) {
136 // Cannot be converted to path (at all)
139 for (String ext
: getImageExt(true)) {
140 File absFile
= new File(absPath
+ ext
);
141 File relFile
= new File(relPath
+ ext
);
142 if (absPath
!= null && absFile
.exists()
143 && absFile
.isFile()) {
144 url
= absFile
.toURI().toURL();
145 } else if (relPath
!= null && relFile
.exists()
146 && relFile
.isFile()) {
147 url
= relFile
.toURI().toURL();
150 } catch (Exception e
) {
151 // Should not happen since we control the correct arguments
158 for (String ext
: getImageExt(true)) {
159 if (Instance
.getCache()
160 .check(new URL(line
+ ext
), true)) {
161 url
= new URL(line
+ ext
);
168 for (String ext
: getImageExt(true)) {
170 url
= new URL(line
+ ext
);
171 Instance
.getCache().refresh(url
, support
, true);
173 } catch (IOException e
) {
174 // no image with this ext
179 } catch (MalformedURLException e
) {
184 // refresh the cached file
187 Instance
.getCache().refresh(url
, support
, true);
188 } catch (IOException e
) {
189 // woops, broken image
199 * Fix the author name if it is prefixed with some "by" {@link String}.
202 * the author with a possible prefix
204 * @return the author without prefixes
206 public String
fixAuthor(String author
) {
207 if (author
!= null) {
208 for (String suffix
: new String
[] { " ", ":" }) {
209 for (String byString
: Instance
.getConfig().getList(Config
.CONF_BYS
)) {
211 if (author
.toUpperCase().startsWith(byString
.toUpperCase())) {
212 author
= author
.substring(byString
.length()).trim();
217 // Special case (without suffix):
218 if (author
.startsWith("©")) {
219 author
= author
.substring(1);