JAL-3603 load annotation file from RELATIVE_URL in JS, small code tidies
[jalview.git] / src / jalview / io / FileParse.java
index 13c0f43..a5a4e36 100755 (executable)
  */
 package jalview.io;
 
-import jalview.api.AlignExportSettingsI;
-import jalview.api.AlignViewportI;
-import jalview.api.AlignmentViewPanel;
-import jalview.api.FeatureSettingsModelI;
-import jalview.util.MessageManager;
-import jalview.util.Platform;
-
 import java.io.BufferedReader;
 import java.io.ByteArrayInputStream;
 import java.io.File;
@@ -41,6 +34,13 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.zip.GZIPInputStream;
 
+import jalview.api.AlignExportSettingsI;
+import jalview.api.AlignViewportI;
+import jalview.api.AlignmentViewPanel;
+import jalview.api.FeatureSettingsModelI;
+import jalview.util.MessageManager;
+import jalview.util.Platform;
+
 /**
  * implements a random access wrapper around a particular datasource, for
  * passing to identifyFile and AlignFile objects.
@@ -332,7 +332,7 @@ public class FileParse
   }
 
   private void parse(File file, String fileStr, DataSourceType sourceType,
-          boolean isFileObject) throws MalformedURLException, IOException
+          boolean isFileObject) throws IOException
   {
     bytes = Platform.getFileBytes(file);
     dataSourceType = sourceType;
@@ -373,7 +373,8 @@ public class FileParse
     {
       // BH 2018 hack for no support for access-origin
       bytes = Platform.getFileAsBytes(fileStr);
-      dataIn = new BufferedReader(new java.io.InputStreamReader(new ByteArrayInputStream(bytes)));      
+      dataIn = new BufferedReader(
+              new InputStreamReader(new ByteArrayInputStream(bytes)));
       dataName = fileStr;
 
     }
@@ -423,8 +424,7 @@ public class FileParse
     else if (sourceType == DataSourceType.CLASSLOADER)
     {
       errormessage = "RESOURCE CANNOT BE LOCATED";
-      java.io.InputStream is = getClass()
-              .getResourceAsStream("/" + fileStr);
+      InputStream is = getClass().getResourceAsStream("/" + fileStr);
       if (is == null)
       {
         String suffixLess = extractSuffix(fileStr);
@@ -435,7 +435,7 @@ public class FileParse
       }
       if (is != null)
       {
-        dataIn = new BufferedReader(new java.io.InputStreamReader(is));
+        dataIn = new BufferedReader(new InputStreamReader(is));
         dataName = fileStr;
       }
       else
@@ -669,4 +669,61 @@ public class FileParse
   {
     return dataSourceType;
   }
+
+  /**
+   * Returns a buffered reader for the input object. Returns null, or throws
+   * IOException, on failure.
+   * 
+   * @param file
+   *          a File, or a String which is a name of a file
+   * @param sourceType
+   * @return
+   * @throws IOException
+   */
+  public BufferedReader getBufferedReader(Object file,
+          DataSourceType sourceType) throws IOException
+  {
+    BufferedReader in = null;
+    byte[] bytes;
+
+    switch (sourceType)
+    {
+    case FILE:
+      if (file instanceof String)
+      {
+        return new BufferedReader(new FileReader((String) file));
+      }
+      bytes = Platform.getFileBytes((File) file);
+      if (bytes != null)
+      {
+        return new BufferedReader(
+                new InputStreamReader(new ByteArrayInputStream(bytes)));
+      }
+      return new BufferedReader(new FileReader((File) file));
+    case URL:
+      URL url = new URL(file.toString());
+      in = new BufferedReader(new InputStreamReader(url.openStream()));
+      break;
+    case RELATIVE_URL: // JalviewJS only
+      bytes = Platform.getFileAsBytes(file.toString());
+      if (bytes != null)
+      {
+        in = new BufferedReader(
+                new InputStreamReader(new ByteArrayInputStream(bytes)));
+      }
+      break;
+    case PASTE:
+      in = new BufferedReader(new StringReader(file.toString()));
+      break;
+    case CLASSLOADER:
+      InputStream is = getClass().getResourceAsStream("/" + file);
+      if (is != null)
+      {
+        in = new BufferedReader(new InputStreamReader(is));
+      }
+      break;
+    }
+
+    return in;
+  }
 }