X-Git-Url: http://git.nikiroo.be/?p=fanfix.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Fsupported%2FBasicSupportHelper.java;h=f3c30bc9657dfe1a70555d8104cd591ad3bf1523;hp=b5c7bb9cdee9ccaf96c1de6c38eeb4aaa76a9537;hb=0a264fbe3d5a43516006052574a5f322d9d38897;hpb=008b697abad5333455de13c773206495ee2b7530 diff --git a/src/be/nikiroo/fanfix/supported/BasicSupportHelper.java b/src/be/nikiroo/fanfix/supported/BasicSupportHelper.java index b5c7bb9..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 @@ -81,7 +85,13 @@ public class BasicSupportHelper { InputStream in = null; try { in = Instance.getInstance().getCache().open(url, support, true); - return new Image(in); + 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) { @@ -220,4 +230,58 @@ public 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; + } }