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);
89 } catch (IOException e
) {
94 } catch (IOException e
) {
104 * Check if the given resource can be a local image or a remote image, then
105 * refresh the cache with it if it is.
108 * the linked {@link BasicSupport} (can be NULL)
110 * the source of the story (for image lookup in the same path if
111 * the source is a file, can be NULL)
113 * the resource to check
115 * @return the image URL if found, or NULL
118 public URL
getImageUrl(BasicSupport support
, URL source
, String line
) {
123 if (source
!= null) {
126 String relPath
= null;
127 String absPath
= null;
129 String path
= new File(source
.getFile()).getParent();
130 relPath
= new File(new File(path
), line
.trim())
132 } catch (Exception e
) {
133 // Cannot be converted to path (one possibility to take
134 // into account: absolute path on Windows)
137 absPath
= new File(line
.trim()).getAbsolutePath();
138 } catch (Exception e
) {
139 // Cannot be converted to path (at all)
142 for (String ext
: getImageExt(true)) {
143 File absFile
= new File(absPath
+ ext
);
144 File relFile
= new File(relPath
+ ext
);
145 if (absPath
!= null && absFile
.exists()
146 && absFile
.isFile()) {
147 url
= absFile
.toURI().toURL();
148 } else if (relPath
!= null && relFile
.exists()
149 && relFile
.isFile()) {
150 url
= relFile
.toURI().toURL();
153 } catch (Exception e
) {
154 // Should not happen since we control the correct arguments
161 for (String ext
: getImageExt(true)) {
162 if (Instance
.getInstance().getCache().check(new URL(line
+ ext
), true)) {
163 url
= new URL(line
+ ext
);
170 for (String ext
: getImageExt(true)) {
172 url
= new URL(line
+ ext
);
173 Instance
.getInstance().getCache().refresh(url
, support
, true);
175 } catch (IOException e
) {
176 // no image with this ext
181 } catch (MalformedURLException e
) {
186 // refresh the cached file
189 Instance
.getInstance().getCache().refresh(url
, support
, true);
190 } catch (IOException e
) {
191 // woops, broken image
201 * Fix the author name if it is prefixed with some "by" {@link String}.
204 * the author with a possible prefix
206 * @return the author without prefixes
208 public String
fixAuthor(String author
) {
209 if (author
!= null) {
210 for (String suffix
: new String
[] { " ", ":" }) {
211 for (String byString
: Instance
.getInstance().getConfig().getList(Config
.CONF_BYS
)) {
213 if (author
.toUpperCase().startsWith(byString
.toUpperCase())) {
214 author
= author
.substring(byString
.length()).trim();
219 // Special case (without suffix):
220 if (author
.startsWith("©")) {
221 author
= author
.substring(1);
229 * Try to convert the date to a known, fixed format.
231 * If it fails to do so, it will return the date as-is.
234 * the date to convert
236 * @return the converted date, or the date as-is
238 public String
formatDate(String date
) {
241 if (date
!= null && !date
.isEmpty()) {
242 // Default Fanfix format:
244 ms
= StringUtils
.toTime(date
);
245 } catch (ParseException e
) {
250 SimpleDateFormat sdf
= new SimpleDateFormat(
251 "yyyy-MM-dd'T'HH:mm:ssSSS");
253 ms
= sdf
.parse(date
).getTime();
254 } catch (ParseException e
) {
259 if (ms
<= 0 && date
.length() >= 10) {
260 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd");
262 ms
= sdf
.parse(date
.substring(0, 10)).getTime();
263 } catch (ParseException e
) {
267 // If we found something, use THIS format:
269 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd");
270 return sdf
.format(new Date(ms
));