X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=inline;f=src%2Fjalview%2Fio%2FIdentifyFile.java;h=baee5314c0f4193245b0aecce67610eb108b0b1c;hb=e57f77dc13f5a295cf49a403da05770a68a6e22b;hp=ed1c29fe0b6f40f3f788022afeb2637bb14e6c77;hpb=2305d687bac5ed8a76ecb93c54c7b090a928e362;p=jalview.git diff --git a/src/jalview/io/IdentifyFile.java b/src/jalview/io/IdentifyFile.java index ed1c29f..baee531 100755 --- a/src/jalview/io/IdentifyFile.java +++ b/src/jalview/io/IdentifyFile.java @@ -20,9 +20,12 @@ */ 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 = ""; + + 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( + "")) + { + 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: [ ...]"); + Console.error("Usage: [ ...]"); } } + }