JAL-3615 used gzip endpoints for Pfam and Rfam
[jalview.git] / src / jalview / io / FileParse.java
index dc0418f..68b5d28 100755 (executable)
@@ -39,12 +39,22 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.zip.GZIPInputStream;
 
+
+import jalview.ws.dbsources.Pfam;
+import jalview.ws.dbsources.Rfam;
+
 /**
  * implements a random access wrapper around a particular datasource, for
  * passing to identifyFile and AlignFile objects.
  */
 public class FileParse
 {
+  protected static final String SPACE = " ";
+
+  protected static final String TAB = "\t";
+
+  private static final String GZ_EXT = ".gz";
+
   /**
    * text specifying source of data. usually filename or url.
    */
@@ -52,6 +62,7 @@ public class FileParse
 
   public File inFile = null;
 
+
   /**
    * a viewport associated with the current file operation. May be null. May
    * move to different object.
@@ -125,9 +136,8 @@ public class FileParse
   {
     if (from == null)
     {
-      throw new Error(
-              MessageManager
-                      .getString("error.implementation_error_null_fileparse"));
+      throw new Error(MessageManager
+              .getString("error.implementation_error_null_fileparse"));
     }
     if (from == this)
     {
@@ -179,7 +189,7 @@ public class FileParse
     }
     if (!error)
     {
-      if (fileStr.toLowerCase().endsWith(".gz"))
+      if (fileStr.toLowerCase().endsWith(GZ_EXT))
       {
         try
         {
@@ -204,16 +214,16 @@ public class FileParse
   private BufferedReader tryAsGzipSource(InputStream inputStream)
           throws Exception
   {
-    BufferedReader inData = new BufferedReader(new InputStreamReader(
-            new GZIPInputStream(inputStream)));
+    BufferedReader inData = new BufferedReader(
+            new InputStreamReader(new GZIPInputStream(inputStream)));
     inData.mark(2048);
     inData.read();
     inData.reset();
     return inData;
   }
 
-  private boolean checkURLSource(String fileStr) throws IOException,
-          MalformedURLException
+  private boolean checkURLSource(String fileStr)
+          throws IOException, MalformedURLException
   {
     errormessage = "URL NOT FOUND";
     URL url = new URL(fileStr);
@@ -221,7 +231,7 @@ public class FileParse
     // GZIPInputStream code borrowed from Aquaria (soon to be open sourced) via
     // Kenny Sabir
     Exception e = null;
-    if (fileStr.toLowerCase().endsWith(".gz"))
+    if (isGzipped(fileStr))
     {
       try
       {
@@ -242,10 +252,8 @@ public class FileParse
     {
       if (e != null)
       {
-        throw new IOException(
-                MessageManager
-                        .getString("exception.failed_to_resolve_gzip_stream"),
-                e);
+        throw new IOException(MessageManager
+                .getString("exception.failed_to_resolve_gzip_stream"), e);
       }
       throw q;
     }
@@ -255,6 +263,27 @@ public class FileParse
   }
 
   /**
+   * Answers true if the filename (or URL) has a format which Jalview recognises
+   * as denoting gzipped content.
+   * <p>
+   * Currently this means having a ".gz" extension, or ending in "/gzipped" or
+   * "?gz=1" (used to retrieve gzipped from Pfam and Rfam respectively).
+   * 
+   * @param filename
+   * @return
+   */
+  protected static boolean isGzipped(String filename)
+  {
+    if (filename == null)
+    {
+      return false;
+    }
+    String lower = filename.toLowerCase();
+    return lower.endsWith(GZ_EXT) || lower.endsWith(Pfam.GZIPPED)
+            || lower.endsWith(Rfam.GZIPPED);
+  }
+
+  /**
    * sets the suffix string (if any) and returns remainder (if suffix was
    * detected)
    * 
@@ -326,15 +355,15 @@ public class FileParse
           {
             throw new IOException(MessageManager.formatMessage(
                     "exception.problem_opening_file_also_tried",
-                    new String[] { inFile.getName(), suffixLess,
-                        errormessage }));
+                    new String[]
+                    { inFile.getName(), suffixLess, errormessage }));
           }
         }
         else
         {
           throw new IOException(MessageManager.formatMessage(
-                  "exception.problem_opening_file",
-                  new String[] { inFile.getName(), errormessage }));
+                  "exception.problem_opening_file", new String[]
+                  { inFile.getName(), errormessage }));
         }
       }
     }
@@ -414,8 +443,8 @@ public class FileParse
     {
       // pass up the reason why we have no source to read from
       throw new IOException(MessageManager.formatMessage(
-              "exception.failed_to_read_data_from_source",
-              new String[] { errormessage }));
+              "exception.failed_to_read_data_from_source", new String[]
+              { errormessage }));
     }
     error = false;
     dataIn.mark(READAHEAD_LIMIT);
@@ -446,9 +475,9 @@ public class FileParse
     {
       return dataIn.readLine();
     }
-    throw new IOException(MessageManager.formatMessage(
-            "exception.invalid_source_stream",
-            new String[] { errormessage }));
+    throw new IOException(MessageManager
+            .formatMessage("exception.invalid_source_stream", new String[]
+            { errormessage }));
   }
 
   /**
@@ -501,9 +530,8 @@ public class FileParse
     }
     else
     {
-      throw new IOException(
-              MessageManager
-                      .getString("error.implementation_error_reset_called_for_invalid_source"));
+      throw new IOException(MessageManager.getString(
+              "error.implementation_error_reset_called_for_invalid_source"));
     }
   }