trailing slash regexed out
[jalview.git] / src / jalview / io / IdentifyFile.java
index 7f3db40..46899da 100755 (executable)
@@ -40,6 +40,7 @@ public class IdentifyFile
    */
   public String Identify(String file, String protocol)
   {
+    String emessage = "UNIDENTIFIED FILE PARSING ERROR";
     FileParse parser = null;
     try {
       parser = new FileParse(file, protocol);
@@ -49,10 +50,11 @@ public class IdentifyFile
     } catch (Exception e) {
       System.err.println("Error whilst identifying");
       e.printStackTrace(System.err);
+      emessage = e.getMessage();
     }
     if (parser!=null)
       return parser.errormessage;
-    return "UNIDENTIFIED FILE PARSING ERROR";
+    return emessage;
   }
   public String Identify(FileParse source) {
     return Identify(source, true); // preserves original behaviour prior to version 2.3
@@ -66,9 +68,42 @@ public class IdentifyFile
   public String Identify(FileParse source, boolean closeSource) {
     String reply = "PFAM";
     String data;
+    int length=0;
+    boolean lineswereskipped=false;
+    boolean isBinary = false; // true if length is non-zero and non-printable characters are encountered
     try {
+      if (!closeSource)
+      {
+        source.mark();
+      }
       while ( (data = source.nextLine()) != null)
       {
+        length+=data.length();
+        if (!lineswereskipped)
+        {
+          for (int i=0;!isBinary && i<data.length(); i++)
+          {
+            char c = data.charAt(i);
+            isBinary = (c<32 && c!='\t' && c!='\n' && c!='\r' && c!=5 && c!=27); // nominal binary character filter excluding CR, LF, tab,DEL and ^E for certain blast ids 
+          }
+        }
+        if (isBinary)
+        {
+          // jar files are special - since they contain all sorts of random characters.
+          if (source.inFile!=null) 
+          {
+              String fileStr=source.inFile.getName();
+              // possibly a Jalview archive. 
+              if (fileStr.lastIndexOf(".jar")>-1 || fileStr.lastIndexOf(".zip")>-1) 
+              {
+                reply = "Jalview";
+              }
+          } 
+          if (!lineswereskipped && data.startsWith("PK")) {
+            reply="Jalview"; // archive.
+            break;
+          }
+        }
         data = data.toUpperCase();
 
         if ( (data.indexOf("# STOCKHOLM") > -1))
@@ -78,8 +113,9 @@ public class IdentifyFile
           break;
         }
 
-        if ( (data.indexOf("#") == 0) || (data.length() < 1))
+        if ((data.length() < 1) || (data.indexOf("#") == 0))
         {
+          lineswereskipped=true;
           continue;
         }
 
@@ -134,10 +170,10 @@ public class IdentifyFile
             }
             else
             {
-              reply = "FASTA";
+                reply = "FASTA";
+                // TODO : AMSA File is indicated if there is annotation in the FASTA file - but FASTA will automatically generate this at the mo.
             }
           }
-
           break;
         }
         else if (data.indexOf("HEADER") == 0 ||
@@ -146,24 +182,17 @@ public class IdentifyFile
           reply = "PDB";
           break;
         }
-        else if (data.indexOf(":") < data.indexOf(",")) //  && data.indexOf(",")<data.indexOf(",", data.indexOf(",")))
+        else if (!lineswereskipped 
+                && data.charAt(0)!='*' 
+                  && data.charAt(0)!=' ' 
+                    && data.indexOf(":") < data.indexOf(",")) //  && data.indexOf(",")<data.indexOf(",", data.indexOf(",")))
         {
           // file looks like a concise JNet file
           reply = "JnetFile";
           break;
         }
-        else  if (source.inFile!=null) 
-        {
-            String fileStr=source.inFile.getName();
-            // possibly a Jalview archive. 
-            if (fileStr.lastIndexOf(".jar")>-1 || fileStr.lastIndexOf(".zip")>-1) 
-            {
-              reply = "Jalview";
-            }
-        } else if (data.startsWith("PK")) {
-          reply="Jalview"; // archive.
-          break;
-        }
+        
+        lineswereskipped=true; // this means there was some junk before any key file signature  
       }
       if (closeSource) {
         source.close();
@@ -176,7 +205,23 @@ public class IdentifyFile
       System.err.println("File Identification failed!\n" + ex);
       return source.errormessage;
     }
-
+    if (length==0)
+    {
+      System.err.println("File Identification failed! - Empty file was read.");
+      return "EMPTY DATA FILE";
+    }
     return reply;
   }
+  public static void main(String[] args) {
+    for (int i=0; args!=null && i<args.length; i++)
+    {
+      IdentifyFile ider = new IdentifyFile();
+      String type = ider.Identify(args[i], AppletFormatAdapter.FILE);
+      System.out.println("Type of "+args[i]+" is "+type);
+    }
+    if (args==null || args.length==0)
+    {
+      System.err.println("Usage: <Filename> [<Filename> ...]");
+    }
+  }
 }