1 package be
.nikiroo
.fanfix
.supported
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
6 import java
.net
.MalformedURLException
;
8 import java
.text
.ParseException
;
9 import java
.text
.SimpleDateFormat
;
10 import java
.util
.Date
;
12 import be
.nikiroo
.fanfix
.Instance
;
13 import be
.nikiroo
.fanfix
.bundles
.Config
;
14 import be
.nikiroo
.utils
.Image
;
15 import be
.nikiroo
.utils
.StringUtils
;
18 * Helper class for {@link BasicSupport}, mostly dedicated to text formating for
19 * the classes that implement {@link BasicSupport}.
23 public class BasicSupportHelper
{
25 * Get the default cover related to this subject (see <tt>.info</tt> files).
30 * @return the cover if any, or NULL
32 public Image
getDefaultCover(String subject
) {
33 if (subject
!= null && !subject
.isEmpty() && Instance
.getInstance().getCoverDir() != null) {
35 File fileCover
= new File(Instance
.getInstance().getCoverDir(), subject
);
36 return getImage(null, fileCover
.toURI().toURL(), subject
);
37 } catch (MalformedURLException e
) {
45 * Return the list of supported image extensions.
48 * TRUE to allow an empty extension on first place, which can be
49 * used when you may already have an extension in your input but
50 * are not sure about it
52 * @return the extensions
54 public String
[] getImageExt(boolean emptyAllowed
) {
56 return new String
[] { "", ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
59 return new String
[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
63 * Check if the given resource can be a local image or a remote image, then
64 * refresh the cache with it if it is.
67 * the linked {@link BasicSupport} (can be NULL)
69 * the source of the story (for image lookup in the same path if
70 * the source is a file, can be NULL)
72 * the resource to check
74 * @return the image if found, or NULL
77 public Image
getImage(BasicSupport support
, URL source
, String line
) {
78 URL url
= getImageUrl(support
, source
, line
);
80 if ("file".equals(url
.getProtocol())) {
81 if (new File(url
.getPath()).isDirectory()) {
85 InputStream in
= null;
87 in
= Instance
.getInstance().getCache().open(url
, support
, true);
88 Image img
= new Image(in
);
89 if (img
.getSize() == 0) {
91 throw new IOException(
92 "Empty image not accepted");
95 } catch (IOException e
) {
100 } catch (IOException e
) {
110 * Check if the given resource can be a local image or a remote image, then
111 * refresh the cache with it if it is.
114 * the linked {@link BasicSupport} (can be NULL)
116 * the source of the story (for image lookup in the same path if
117 * the source is a file, can be NULL)
119 * the resource to check
121 * @return the image URL if found, or NULL
124 public URL
getImageUrl(BasicSupport support
, URL source
, String line
) {
129 if (source
!= null) {
132 String relPath
= null;
133 String absPath
= null;
135 String path
= new File(source
.getFile()).getParent();
136 relPath
= new File(new File(path
), line
.trim())
138 } catch (Exception e
) {
139 // Cannot be converted to path (one possibility to take
140 // into account: absolute path on Windows)
143 absPath
= new File(line
.trim()).getAbsolutePath();
144 } catch (Exception e
) {
145 // Cannot be converted to path (at all)
148 for (String ext
: getImageExt(true)) {
149 File absFile
= new File(absPath
+ ext
);
150 File relFile
= new File(relPath
+ ext
);
151 if (absPath
!= null && absFile
.exists()
152 && absFile
.isFile()) {
153 url
= absFile
.toURI().toURL();
154 } else if (relPath
!= null && relFile
.exists()
155 && relFile
.isFile()) {
156 url
= relFile
.toURI().toURL();
159 } catch (Exception e
) {
160 // Should not happen since we control the correct arguments
167 for (String ext
: getImageExt(true)) {
168 if (Instance
.getInstance().getCache().check(new URL(line
+ ext
), true)) {
169 url
= new URL(line
+ ext
);
176 for (String ext
: getImageExt(true)) {
178 url
= new URL(line
+ ext
);
179 Instance
.getInstance().getCache().refresh(url
, support
, true);
181 } catch (IOException e
) {
182 // no image with this ext
187 } catch (MalformedURLException e
) {
192 // refresh the cached file
195 Instance
.getInstance().getCache().refresh(url
, support
, true);
196 } catch (IOException e
) {
197 // woops, broken image
207 * Fix the author name if it is prefixed with some "by" {@link String}.
210 * the author with a possible prefix
212 * @return the author without prefixes
214 public String
fixAuthor(String author
) {
215 if (author
!= null) {
216 for (String suffix
: new String
[] { " ", ":" }) {
217 for (String byString
: Instance
.getInstance().getConfig().getList(Config
.CONF_BYS
)) {
219 if (author
.toUpperCase().startsWith(byString
.toUpperCase())) {
220 author
= author
.substring(byString
.length()).trim();
225 // Special case (without suffix):
226 if (author
.startsWith("©")) {
227 author
= author
.substring(1);
235 * Try to convert the date to a known, fixed format.
237 * If it fails to do so, it will return the date as-is.
240 * the date to convert
242 * @return the converted date, or the date as-is
244 public String
formatDate(String date
) {
247 if (date
!= null && !date
.isEmpty()) {
248 // Default Fanfix format:
250 ms
= StringUtils
.toTime(date
);
251 } catch (ParseException e
) {
256 SimpleDateFormat sdf
= new SimpleDateFormat(
257 "yyyy-MM-dd'T'HH:mm:ssSSS");
259 ms
= sdf
.parse(date
).getTime();
260 } catch (ParseException e
) {
265 if (ms
<= 0 && date
.length() >= 10) {
266 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd");
268 ms
= sdf
.parse(date
.substring(0, 10)).getTime();
269 } catch (ParseException e
) {
273 // If we found something, use THIS format:
275 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd");
276 return sdf
.format(new Date(ms
));