X-Git-Url: http://git.nikiroo.be/?p=fanfix.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Fsupported%2FBasicSupportHelper.java;h=f3c30bc9657dfe1a70555d8104cd591ad3bf1523;hp=652506762ff6b9cc5aa13f19836d40d04e16bd42;hb=0a264fbe3d5a43516006052574a5f322d9d38897;hpb=b7cd9db81ba27c67fdd8dd5a42b5f9e4137622db diff --git a/src/be/nikiroo/fanfix/supported/BasicSupportHelper.java b/src/be/nikiroo/fanfix/supported/BasicSupportHelper.java index 6525067..f3c30bc 100644 --- a/src/be/nikiroo/fanfix/supported/BasicSupportHelper.java +++ b/src/be/nikiroo/fanfix/supported/BasicSupportHelper.java @@ -5,10 +5,14 @@ import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.bundles.Config; import be.nikiroo.utils.Image; +import be.nikiroo.utils.StringUtils; /** * Helper class for {@link BasicSupport}, mostly dedicated to text formating for @@ -16,7 +20,7 @@ import be.nikiroo.utils.Image; * * @author niki */ -class BasicSupportHelper { +public class BasicSupportHelper { /** * Get the default cover related to this subject (see .info files). * @@ -25,11 +29,10 @@ class BasicSupportHelper { * * @return the cover if any, or NULL */ - public static Image getDefaultCover(String subject) { - if (subject != null && !subject.isEmpty() - && Instance.getCoverDir() != null) { + public Image getDefaultCover(String subject) { + if (subject != null && !subject.isEmpty() && Instance.getInstance().getCoverDir() != null) { try { - File fileCover = new File(Instance.getCoverDir(), subject); + File fileCover = new File(Instance.getInstance().getCoverDir(), subject); return getImage(null, fileCover.toURI().toURL(), subject); } catch (MalformedURLException e) { } @@ -48,7 +51,7 @@ class BasicSupportHelper { * * @return the extensions */ - public static String[] getImageExt(boolean emptyAllowed) { + public String[] getImageExt(boolean emptyAllowed) { if (emptyAllowed) { return new String[] { "", ".png", ".jpg", ".jpeg", ".gif", ".bmp" }; } @@ -61,16 +64,17 @@ class BasicSupportHelper { * refresh the cache with it if it is. * * @param support - * the linked {@link BasicSupport} + * the linked {@link BasicSupport} (can be NULL) * @param source - * the story source + * the source of the story (for image lookup in the same path if + * the source is a file, can be NULL) * @param line * the resource to check * * @return the image if found, or NULL * */ - public static Image getImage(BasicSupport support, URL source, String line) { + public Image getImage(BasicSupport support, URL source, String line) { URL url = getImageUrl(support, source, line); if (url != null) { if ("file".equals(url.getProtocol())) { @@ -80,8 +84,14 @@ class BasicSupportHelper { } InputStream in = null; try { - in = Instance.getCache().open(url, support, true); - return new Image(in); + in = Instance.getInstance().getCache().open(url, support, true); + Image img = new Image(in); + if (img.getSize() == 0) { + img.close(); + throw new IOException( + "Empty image not accepted"); + } + return img; } catch (IOException e) { } finally { if (in != null) { @@ -101,16 +111,17 @@ class BasicSupportHelper { * refresh the cache with it if it is. * * @param support - * the linked {@link BasicSupport} + * the linked {@link BasicSupport} (can be NULL) * @param source - * the story source + * the source of the story (for image lookup in the same path if + * the source is a file, can be NULL) * @param line * the resource to check * * @return the image URL if found, or NULL * */ - public static URL getImageUrl(BasicSupport support, URL source, String line) { + public URL getImageUrl(BasicSupport support, URL source, String line) { URL url = null; if (line != null) { @@ -154,8 +165,7 @@ class BasicSupportHelper { // try for URLs try { for (String ext : getImageExt(true)) { - if (Instance.getCache() - .check(new URL(line + ext), true)) { + if (Instance.getInstance().getCache().check(new URL(line + ext), true)) { url = new URL(line + ext); break; } @@ -166,7 +176,7 @@ class BasicSupportHelper { for (String ext : getImageExt(true)) { try { url = new URL(line + ext); - Instance.getCache().refresh(url, support, true); + Instance.getInstance().getCache().refresh(url, support, true); break; } catch (IOException e) { // no image with this ext @@ -182,7 +192,7 @@ class BasicSupportHelper { // refresh the cached file if (url != null) { try { - Instance.getCache().refresh(url, support, true); + Instance.getInstance().getCache().refresh(url, support, true); } catch (IOException e) { // woops, broken image url = null; @@ -201,10 +211,10 @@ class BasicSupportHelper { * * @return the author without prefixes */ - public static String fixAuthor(String author) { + public String fixAuthor(String author) { if (author != null) { for (String suffix : new String[] { " ", ":" }) { - for (String byString : Instance.getConfig().getList(Config.BYS)) { + for (String byString : Instance.getInstance().getConfig().getList(Config.CONF_BYS)) { byString += suffix; if (author.toUpperCase().startsWith(byString.toUpperCase())) { author = author.substring(byString.length()).trim(); @@ -220,4 +230,58 @@ class BasicSupportHelper { return author; } + + /** + * Try to convert the date to a known, fixed format. + *

+ * If it fails to do so, it will return the date as-is. + * + * @param date + * the date to convert + * + * @return the converted date, or the date as-is + */ + public String formatDate(String date) { + long ms = 0; + + if (date != null && !date.isEmpty()) { + // Default Fanfix format: + try { + ms = StringUtils.toTime(date); + } catch (ParseException e) { + } + + // Second chance: + if (ms <= 0) { + SimpleDateFormat sdf = new SimpleDateFormat( + "yyyy-MM-dd'T'HH:mm:ssSSS"); + try { + ms = sdf.parse(date).getTime(); + } catch (ParseException e) { + } + } + + // Last chance: + if (ms <= 0 && date.length() >= 10) { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + try { + ms = sdf.parse(date.substring(0, 10)).getTime(); + } catch (ParseException e) { + } + } + + // If we found something, use THIS format: + if (ms > 0) { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + return sdf.format(new Date(ms)); + } + } + + if (date == null) { + date = ""; + } + + // :( + return date; + } }