X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=supported%2FBasicSupportHelper.java;h=7768052cafc758094e38c1968f5919f6b853b8bc;hb=75a6a3eadc87e42bb0c9808e359d23d03801a9a2;hp=41716df4e4d17952cd0e77aeac28eee20e837aca;hpb=0fc81e6465aa9c1f1dfc19b532082220d609768a;p=nikiroo-utils.git diff --git a/supported/BasicSupportHelper.java b/supported/BasicSupportHelper.java index 41716df..7768052 100644 --- a/supported/BasicSupportHelper.java +++ b/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 @@ -26,10 +30,9 @@ public class BasicSupportHelper { * @return the cover if any, or NULL */ public Image getDefaultCover(String subject) { - if (subject != null && !subject.isEmpty() - && Instance.getCoverDir() != null) { + 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) { } @@ -81,7 +84,7 @@ public class BasicSupportHelper { } InputStream in = null; try { - in = Instance.getCache().open(url, support, true); + in = Instance.getInstance().getCache().open(url, support, true); return new Image(in); } catch (IOException e) { } finally { @@ -156,8 +159,7 @@ public 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; } @@ -168,7 +170,7 @@ public 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 @@ -184,7 +186,7 @@ public 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; @@ -206,7 +208,7 @@ public class BasicSupportHelper { public String fixAuthor(String author) { if (author != null) { for (String suffix : new String[] { " ", ":" }) { - for (String byString : Instance.getConfig().getList(Config.CONF_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(); @@ -222,4 +224,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; + } }