WIP: convert local "supports" to new BasicSupport
[fanfix.git] / src / be / nikiroo / fanfix / supported / InfoReader.java
index 571f77b0a1cda5b08e6e775b257d9db8f27eb101..8e1c385cfa8d115fd9fd6035be77eac10c580611 100644 (file)
@@ -8,6 +8,7 @@ import java.io.InputStream;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Scanner;
 
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.bundles.Config;
@@ -29,13 +30,12 @@ public class InfoReader {
                                return createMeta(infoFile.toURI().toURL(), in, withCover);
                        } finally {
                                in.close();
-                               in = null;
                        }
-               } else {
-                       throw new FileNotFoundException(
-                                       "File given as argument does not exists: "
-                                                       + infoFile.getAbsolutePath());
                }
+
+               throw new FileNotFoundException(
+                               "File given as argument does not exists: "
+                                               + infoFile.getAbsolutePath());
        }
 
        private static MetaData createMeta(URL sourceInfoFile, InputStream in,
@@ -56,18 +56,22 @@ public class InfoReader {
                meta.setType(getInfoTag(in, "TYPE"));
                meta.setImageDocument(getInfoTagBoolean(in, "IMAGES_DOCUMENT", false));
                if (withCover) {
-                       meta.setCover(BasicSupport.getImage(null, sourceInfoFile,
-                                       getInfoTag(in, "COVER")));
+                       String infoTag = getInfoTag(in, "COVER");
+                       if (infoTag != null && !infoTag.trim().isEmpty()) {
+                               meta.setCover(BasicSupportHelper.getImage(null, sourceInfoFile,
+                                               infoTag));
+                       }
                        // Second chance: try to check for a cover next to the info file
                        if (meta.getCover() == null) {
                                String info = sourceInfoFile.getFile().toString();
                                if (info.endsWith(".info")) {
                                        info = info.substring(0, info.length() - ".info".length());
                                        String ext = "."
-                                                       + Instance.getConfig().getString(
-                                                                       Config.IMAGE_FORMAT_COVER);
-                                       meta.setCover(BasicSupport.getImage(null, sourceInfoFile,
-                                                       info + ext));
+                                                       + Instance.getConfig()
+                                                                       .getString(Config.IMAGE_FORMAT_COVER)
+                                                                       .toLowerCase();
+                                       meta.setCover(BasicSupportHelper.getImage(null,
+                                                       sourceInfoFile, info + ext));
                                }
                        }
                }
@@ -80,7 +84,7 @@ public class InfoReader {
                meta.setFakeCover(Boolean.parseBoolean(getInfoTag(in, "FAKE_COVER")));
 
                if (withCover && meta.getCover() == null) {
-                       meta.setCover(BasicSupport.getDefaultCover(meta.getSubject()));
+                       meta.setCover(BasicSupportHelper.getDefaultCover(meta.getSubject()));
                }
 
                return meta;
@@ -134,7 +138,7 @@ public class InfoReader {
 
                if (in != null) {
                        in.reset();
-                       String value = BasicSupport.getLine(in, key, 0);
+                       String value = getLine(in, key, 0);
                        if (value != null && !value.isEmpty()) {
                                value = value.trim().substring(key.length() - 1).trim();
                                if (value.startsWith("'") && value.endsWith("'")
@@ -148,4 +152,81 @@ public class InfoReader {
 
                return null;
        }
+
+       /**
+        * Return the first line from the given input which correspond to the given
+        * selectors.
+        * 
+        * @param in
+        *            the input
+        * @param needle
+        *            a string that must be found inside the target line (also
+        *            supports "^" at start to say "only if it starts with" the
+        *            needle)
+        * @param relativeLine
+        *            the line to return based upon the target line position (-1 =
+        *            the line before, 0 = the target line...)
+        * 
+        * @return the line
+        */
+       static private String getLine(InputStream in, String needle,
+                       int relativeLine) {
+               return getLine(in, needle, relativeLine, true);
+       }
+
+       /**
+        * Return a line from the given input which correspond to the given
+        * selectors.
+        * 
+        * @param in
+        *            the input
+        * @param needle
+        *            a string that must be found inside the target line (also
+        *            supports "^" at start to say "only if it starts with" the
+        *            needle)
+        * @param relativeLine
+        *            the line to return based upon the target line position (-1 =
+        *            the line before, 0 = the target line...)
+        * @param first
+        *            takes the first result (as opposed to the last one, which will
+        *            also always spend the input)
+        * 
+        * @return the line
+        */
+       static private String getLine(InputStream in, String needle,
+                       int relativeLine, boolean first) {
+               String rep = null;
+
+               List<String> lines = new ArrayList<String>();
+               @SuppressWarnings("resource")
+               Scanner scan = new Scanner(in, "UTF-8");
+               int index = -1;
+               scan.useDelimiter("\\n");
+               while (scan.hasNext()) {
+                       lines.add(scan.next());
+
+                       if (index == -1) {
+                               if (needle.startsWith("^")) {
+                                       if (lines.get(lines.size() - 1).startsWith(
+                                                       needle.substring(1))) {
+                                               index = lines.size() - 1;
+                                       }
+
+                               } else {
+                                       if (lines.get(lines.size() - 1).contains(needle)) {
+                                               index = lines.size() - 1;
+                                       }
+                               }
+                       }
+
+                       if (index >= 0 && index + relativeLine < lines.size()) {
+                               rep = lines.get(index + relativeLine);
+                               if (first) {
+                                       break;
+                               }
+                       }
+               }
+
+               return rep;
+       }
 }