X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FAppletFormatAdapter.java;h=635c80175fcc3a574f3a0abb4fe8ec106016b90a;hb=a45774ee31d9f35d4eff46d54d7deab719afb092;hp=5301ab6fef6e47ffa96999e863f508949a4035ba;hpb=75963e12376e3dbfc94e6324461024baea68178f;p=jalview.git diff --git a/src/jalview/io/AppletFormatAdapter.java b/src/jalview/io/AppletFormatAdapter.java index 5301ab6..635c801 100755 --- a/src/jalview/io/AppletFormatAdapter.java +++ b/src/jalview/io/AppletFormatAdapter.java @@ -1,6 +1,6 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5) - * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle + * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7) + * Copyright (C) 2011 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle * * This file is part of Jalview. * @@ -18,6 +18,7 @@ package jalview.io; import java.io.File; +import java.io.InputStream; import jalview.datamodel.*; @@ -65,18 +66,22 @@ public class AppletFormatAdapter * corresponding to READABLE_FNAMES */ public static final String[] READABLE_EXTENSIONS = new String[] - { "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc", "amsa", "jar", "sto" }; // , - // ".blast" - // }; + { "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc", "amsa", "jar", + "sto" }; // , + + // ".blast" + // }; /** * List of readable formats by application in order corresponding to * READABLE_EXTENSIONS */ public static final String[] READABLE_FNAMES = new String[] - { "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA", "Jalview", "Stockholm" };// , - // "SimpleBLAST" - // }; + { "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA", "Jalview", + "Stockholm" };// , + + // "SimpleBLAST" + // }; public static String INVALID_CHARACTERS = "Contains invalid characters"; @@ -112,7 +117,18 @@ public class AppletFormatAdapter AlignFile afile = null; String inFile; - + /** + * character used to write newlines + */ + protected String newline = System.getProperty("line.separator"); + public void setNewlineString(String nl) + { + newline = nl; + } + public String getNewlineString() + { + return newline; + } /** * check that this format is valid for reading * @@ -436,7 +452,7 @@ public class AppletFormatAdapter throw new Exception( "Implementation error: Unknown file format string"); } - + afile.setNewlineString(newline); afile.addJVSuffix(jvsuffix); afile.setSeqs(alignment.getSequencesArray()); @@ -458,6 +474,17 @@ public class AppletFormatAdapter return null; } + public static String checkProtocol(String file) + { + String protocol = FILE; + String ft = file.toLowerCase().trim(); + if (ft.indexOf("http:") ==0 || ft.indexOf("https:") ==0 || ft.indexOf("file:") == 0) + { + protocol = URL; + } + return protocol; + } + public static void main(String[] args) { int i = 0; @@ -474,8 +501,8 @@ public class AppletFormatAdapter System.gc(); long memf = -r.totalMemory() + r.freeMemory(); long t1 = -System.currentTimeMillis(); - Alignment al = afa.readFile(args[i], FILE, new IdentifyFile() - .Identify(args[i], FILE)); + Alignment al = afa.readFile(args[i], FILE, + new IdentifyFile().Identify(args[i], FILE)); t1 += System.currentTimeMillis(); System.gc(); memf += r.totalMemory() - r.freeMemory(); @@ -516,6 +543,176 @@ public class AppletFormatAdapter } i++; } + } + + /** + * try to discover how to access the given file as a valid datasource that + * will be identified as the given type. + * + * @param file + * @param format + * @return protocol that yields the data parsable as the given type + */ + public static String resolveProtocol(String file, String format) + { + return resolveProtocol(file, format, false); + } + + public static String resolveProtocol(String file, String format, + boolean debug) + { + // TODO: test thoroughly! + String protocol = null; + if (debug) + { + System.out.println("resolving datasource started with:\n>>file\n" + + file + ">>endfile"); + } + + // This might throw a security exception in certain browsers + // Netscape Communicator for instance. + try + { + boolean rtn = false; + InputStream is = System.getSecurityManager().getClass() + .getResourceAsStream("/" + file); + if (is != null) + { + rtn = true; + is.close(); + } + if (debug) + { + System.err.println("Resource '" + file + "' was " + + (rtn ? "" : "not") + " located by classloader."); + } + ; + if (rtn) + { + protocol = AppletFormatAdapter.CLASSLOADER; + } + + } catch (Exception ex) + { + System.err + .println("Exception checking resources: " + file + " " + ex); + } + if (file.indexOf("://") > -1) + { + protocol = AppletFormatAdapter.URL; + } + else + { + // skipping codebase prepend check. + protocol = AppletFormatAdapter.FILE; + } + FileParse fp = null; + try + { + if (debug) + { + System.out.println("Trying to get contents of resource as " + + protocol + ":"); + } + fp = new FileParse(file, protocol); + if (!fp.isValid()) + { + fp = null; + } + else + { + if (debug) + { + System.out.println("Successful."); + } + } + } catch (Exception e) + { + if (debug) + { + System.err.println("Exception when accessing content: " + e); + } + fp = null; + } + if (fp == null) + { + if (debug) + { + System.out.println("Accessing as paste."); + } + protocol = AppletFormatAdapter.PASTE; + fp = null; + try + { + fp = new FileParse(file, protocol); + if (!fp.isValid()) + { + fp = null; + } + } catch (Exception e) + { + System.err.println("Failed to access content as paste!"); + e.printStackTrace(); + fp = null; + } + } + if (fp == null) + { + return null; + } + if (format == null || format.length() == 0) + { + return protocol; + } + else + { + try + { + String idformat = new jalview.io.IdentifyFile().Identify(file, + protocol); + if (idformat == null) + { + if (debug) + { + System.out.println("Format not identified. Inaccessible file."); + } + return null; + } + if (debug) + { + System.out.println("Format identified as " + idformat + + "and expected as " + format); + } + if (idformat.equals(format)) + { + if (debug) + { + System.out.println("Protocol identified as " + protocol); + } + return protocol; + } + else + { + if (debug) + { + System.out + .println("File deemed not accessible via " + protocol); + } + fp.close(); + return null; + } + } catch (Exception e) + { + if (debug) + { + System.err.println("File deemed not accessible via " + protocol); + e.printStackTrace(); + } + ; + + } + } + return null; } }