JAL-1645 source formatting and organise imports
[jalview.git] / src / jalview / io / IdentifyFile.java
index 25cef72..ff0f0cc 100755 (executable)
@@ -30,6 +30,8 @@ import java.io.IOException;
  */
 public class IdentifyFile
 {
+  public static final String GFF3File = "GFF v2 or v3";
+
   /**
    * Identify a datasource's file content.
    *
@@ -96,7 +98,7 @@ public class IdentifyFile
       }
       while ((data = source.nextLine()) != null)
       {
-        length += data.length();
+        length += data.trim().length();
         if (!lineswereskipped)
         {
           for (int i = 0; !isBinary && i < data.length(); i++)
@@ -130,55 +132,18 @@ public class IdentifyFile
         }
         data = data.toUpperCase();
 
-        if ((data.indexOf("# STOCKHOLM") > -1))
-        {
-          reply = "STH";
-
-          break;
-        }
-        // if (data.matches("<(\"[^\"]*\"|'[^']*'|[^'\">])*>"))
-        if (data.matches("<(?i)html(\"[^\"]*\"|'[^']*'|[^'\">])*>"))
+        if (data.startsWith("##GFF-VERSION"))
         {
-          reply = "HTML";
-          break;
-        }
-
-        if (data.matches("<(?i)rnaml (\"[^\"]*\"|'[^']*'|[^'\">])*>"))
-        {
-          reply = "RNAML";
-
+          reply = GFF3File;
           break;
         }
-
-        if ((data.length() < 1) || (data.indexOf("#") == 0))
+        if (data.indexOf("# STOCKHOLM") > -1)
         {
-          lineswereskipped = true;
-          continue;
-        }
-
-        if (data.indexOf("PILEUP") > -1)
-        {
-          reply = "PileUp";
-
-          break;
-        }
-
-        if ((data.indexOf("//") == 0)
-                || ((data.indexOf("!!") > -1) && (data.indexOf("!!") < data
-                        .indexOf("_MULTIPLE_ALIGNMENT "))))
-        {
-          reply = "MSF";
-
-          break;
-        }
-        else if (data.indexOf("CLUSTAL") > -1)
-        {
-          reply = "CLUSTAL";
-
+          reply = "STH";
           break;
         }
-
-        else if (data.indexOf(">") > -1)
+        // if (data.indexOf(">") > -1)
+        if (data.startsWith(">"))
         {
           // FASTA, PIR file or BLC file
           boolean checkPIR = false, starterm = false;
@@ -270,6 +235,66 @@ public class IdentifyFile
           // read as a FASTA (probably)
           break;
         }
+        if ((data.indexOf("<") > -1)) // possible Markup Language data i.e HTML,
+                                      // RNAML, XML
+        {
+          boolean identified = false;
+          do
+          {
+            if (data.matches("<(?i)html(\"[^\"]*\"|'[^']*'|[^'\">])*>"))
+            {
+              reply = HtmlFile.FILE_DESC;
+              identified = true;
+              break;
+            }
+
+            if (data.matches("<(?i)rnaml (\"[^\"]*\"|'[^']*'|[^'\">])*>"))
+            {
+              reply = "RNAML";
+              identified = true;
+              break;
+            }
+          } while ((data = source.nextLine()) != null);
+
+          if (identified)
+          {
+            break;
+          }
+        }
+
+        if (data.indexOf("{\"") > -1)
+        {
+          reply = JSONFile.FILE_DESC;
+          break;
+        }
+        if ((data.length() < 1) || (data.indexOf("#") == 0))
+        {
+          lineswereskipped = true;
+          continue;
+        }
+
+        if (data.indexOf("PILEUP") > -1)
+        {
+          reply = "PileUp";
+
+          break;
+        }
+
+        if ((data.indexOf("//") == 0)
+                || ((data.indexOf("!!") > -1) && (data.indexOf("!!") < data
+                        .indexOf("_MULTIPLE_ALIGNMENT "))))
+        {
+          reply = "MSF";
+
+          break;
+        }
+        else if (data.indexOf("CLUSTAL") > -1)
+        {
+          reply = "CLUSTAL";
+
+          break;
+        }
+
         else if (data.indexOf("HEADER") == 0 || data.indexOf("ATOM") == 0)
         {
           reply = "PDB";
@@ -281,19 +306,18 @@ public class IdentifyFile
           break;
         }
 
-
         /*
          * // TODO comment out SimpleBLAST identification for Jalview 2.4.1 else
          * if (!lineswereskipped && data.indexOf("BLAST")<4) { reply =
          * "SimpleBLAST"; break;
-         *
+         * 
          * } // end comments for Jalview 2.4.1
          */
         else if (!lineswereskipped && data.charAt(0) != '*'
                 && data.charAt(0) != ' '
                 && data.indexOf(":") < data.indexOf(",")) // &&
-          // data.indexOf(",")<data.indexOf(",",
-          // data.indexOf(",")))
+        // data.indexOf(",")<data.indexOf(",",
+        // data.indexOf(",")))
         {
           // file looks like a concise JNet file
           reply = "JnetFile";
@@ -319,7 +343,7 @@ public class IdentifyFile
     if (length == 0)
     {
       System.err
-      .println("File Identification failed! - Empty file was read.");
+              .println("File Identification failed! - Empty file was read.");
       return "EMPTY DATA FILE";
     }
     return reply;