JAL-4090 JAL-1551 spotlessApply
[jalview.git] / src / jalview / io / IdentifyFile.java
index ed1c29f..baee531 100755 (executable)
  */
 package jalview.io;
 
+import java.io.File;
 import java.io.IOException;
 import java.util.Locale;
 
+import jalview.bin.Console;
+
 /**
  * DOCUMENT ME!
  *
@@ -31,6 +34,43 @@ import java.util.Locale;
  */
 public class IdentifyFile
 {
+
+  private static final String XMLHEADER = "<?XML VERSION=\"1.0\" ENCODING=\"UTF-8\" STANDALONE=\"YES\"?>";
+
+  public FileFormatI identify(Object file, DataSourceType protocol)
+          throws FileFormatException
+  {
+    // BH 2018
+    return (file instanceof File ? identify((File) file, protocol)
+            : identify((String) file, protocol));
+
+  }
+
+  public FileFormatI identify(File file, DataSourceType sourceType)
+          throws FileFormatException
+  {
+    // BH 2018
+    String emessage = "UNIDENTIFIED FILE PARSING ERROR";
+    FileParse parser = null;
+    try
+    {
+      parser = new FileParse(file, sourceType);
+      if (parser.isValid())
+      {
+        return identify(parser);
+      }
+    } catch (Exception e)
+    {
+      Console.error("Error whilst identifying " + file, e);
+      emessage = e.getMessage();
+    }
+    if (parser != null)
+    {
+      throw new FileFormatException(parser.errormessage);
+    }
+    throw new FileFormatException(emessage);
+  }
+
   /**
    * Identify a datasource's file content.
    *
@@ -56,8 +96,7 @@ public class IdentifyFile
       }
     } catch (Exception e)
     {
-      System.err.println("Error whilst identifying");
-      e.printStackTrace(System.err);
+      Console.error("Error whilst identifying " + file, e);
       emessage = e.getMessage();
     }
     if (parser != null)
@@ -97,6 +136,7 @@ public class IdentifyFile
     String data;
     int bytesRead = 0;
     int trimmedLength = 0;
+    boolean isXml = false; // set true if first line is XMLHEADER
     boolean lineswereskipped = false;
     boolean isBinary = false; // true if length is non-zero and non-printable
     // characters are encountered
@@ -131,16 +171,16 @@ public class IdentifyFile
           if (source.inFile != null)
           {
             String fileStr = source.inFile.getName();
-            // possibly a Jalview archive.
-            if (fileStr.lastIndexOf(".jar") > -1
-                    || fileStr.lastIndexOf(".zip") > -1)
+            if (fileStr.contains(".jar") || fileStr.contains(".zip")
+                    || fileStr.contains(".jvp"))
             {
+              // possibly a Jalview archive (but check further)
               reply = FileFormat.Jalview;
             }
           }
           if (!lineswereskipped && data.startsWith("PK"))
           {
-            reply = FileFormat.Jalview; // archive.
+            reply = FileFormat.Jalview; // archive
             break;
           }
         }
@@ -151,6 +191,23 @@ public class IdentifyFile
           reply = FileFormat.ScoreMatrix;
           break;
         }
+        if (data.startsWith(XMLHEADER) && !lineswereskipped)
+        {
+          isXml = true;
+        }
+        if (data.startsWith("LOCUS"))
+        {
+          reply = FileFormat.GenBank;
+          break;
+        }
+        if (data.startsWith("ID "))
+        {
+          if (data.substring(2).trim().split(";").length == 7)
+          {
+            reply = FileFormat.Embl;
+            break;
+          }
+        }
         if (data.startsWith("H ") && !aaIndexHeaderRead)
         {
           aaIndexHeaderRead = true;
@@ -296,6 +353,12 @@ public class IdentifyFile
             reply = FileFormat.Rnaml;
             break;
           }
+          if (isXml && data.contains(
+                  "<NS2:JALVIEWUSERCOLOURS SCHEMENAME=\"SEQUENCE FEATURES\" XMLNS:NS2=\"WWW.JALVIEW.ORG/COLOURS\">"))
+          {
+            reply = FileFormat.FeatureSettings;
+            break;
+          }
         }
 
         if ((data.length() < 1) || (data.indexOf("#") == 0))
@@ -357,16 +420,15 @@ public class IdentifyFile
       }
     } catch (Exception ex)
     {
-      System.err.println("File Identification failed!\n" + ex);
+      Console.error("File Identification failed!\n" + ex);
       throw new FileFormatException(source.errormessage);
     }
     if (trimmedLength == 0)
     {
-      System.err.println(
-              "File Identification failed! - Empty file was read.");
+      Console.error("File Identification failed! - Empty file was read.");
       throw new FileFormatException("EMPTY DATA FILE");
     }
-    System.out.println("File format identified as " + reply.toString());
+    Console.debug("File format identified as " + reply.toString());
     return reply;
   }
 
@@ -418,6 +480,11 @@ public class IdentifyFile
     return true;
   }
 
+  /**
+   * 
+   * @param args
+   * @j2sIgnore
+   */
   public static void main(String[] args)
   {
     for (int i = 0; args != null && i < args.length; i++)
@@ -429,15 +496,16 @@ public class IdentifyFile
         type = ider.identify(args[i], DataSourceType.FILE);
       } catch (FileFormatException e)
       {
-        System.err.println(
+        Console.error(
                 String.format("Error '%s' identifying file type for %s",
                         args[i], e.getMessage()));
       }
-      System.out.println("Type of " + args[i] + " is " + type);
+      Console.debug("Type of " + args[i] + " is " + type);
     }
     if (args == null || args.length == 0)
     {
-      System.err.println("Usage: <Filename> [<Filename> ...]");
+      Console.error("Usage: <Filename> [<Filename> ...]");
     }
   }
+
 }