3 import jalview.datamodel.AlignmentI;
4 import jalview.io.AlignFile;
5 import jalview.io.FileParse;
7 import java.io.BufferedReader;
8 import java.io.FileReader;
9 import java.io.IOException;
10 import java.io.InputStreamReader;
11 import java.lang.reflect.Constructor;
12 import java.lang.reflect.InvocationTargetException;
15 import javajs.J2SIgnoreImport;
17 * A class to open files via reflection so that their classes are only loaded as necessary
19 * note: not all file openers have corresponding String inFile and FileParse
20 * source options. Why not?
22 * note: Pileupfile does not have a capital "F"; the method below requires that,
23 * so if that file name gets changed, so too does the reflection code here.
29 @J2SIgnoreImport({java.io.FileReader.class})
30 abstract public class GenericFileAdapter extends AlignFile {
33 * inFileOrSource class type
35 * @param inFileOrSource
36 * type will determine constructor -- [], [AlignmentI],
37 * [inFile,type], or [source]
42 public static AlignFile getFile(String fileType, Object... params) {
45 cl = Class.forName("jalview.io." + fileType);
46 } catch (ClassNotFoundException e) {
47 System.err.println("did not find file jalview.io." + fileType);
53 switch (params.length) {
55 return (AlignFile) cl.newInstance();
57 m = (params[0] instanceof FileParse ? cl
58 .getConstructor(FileParse.class) : cl
59 .getConstructor(AlignmentI.class));
62 m = cl.getConstructor(String.class, String.class);
67 return (AlignFile) m.newInstance(params);
68 } catch (InstantiationException e) {
70 } catch (IllegalAccessException e) {
72 } catch (NoSuchMethodException e) {
74 } catch (SecurityException e) {
76 } catch (IllegalArgumentException e) {
78 } catch (InvocationTargetException e) {
82 System.err.println("Error in GenericFileAdapter: " + ex);
97 * Determines whether or not we have a JavaScript applet.
101 public static boolean isJS() {
114 * opens a file for line-oriented reading via File() or URL()
119 * @throws IOException
121 public static BufferedReader getReader(String fileName, boolean forceURL) throws IOException {
122 if (!forceURL && !isJS())
123 return new BufferedReader(new FileReader(fileName));
124 if (fileName.indexOf("//") < 0)
125 fileName = "file://" + fileName;
126 return new BufferedReader(new InputStreamReader(new URL(fileName).openStream()));
129 public final static String TCOFFEE_SCORE = "TCoffeeScore";
130 public static final int Phylip_FILE = 1;
131 public static final String Phylip_FILE_EXT = "phy";
132 public static final String Phylip_FILE_DESC = "PHYLIP";
133 public static final int JSON_FILE = 2;
134 public static final String JSON_FILE_EXT = "json";
135 public static final String JSON_FILE_DESC = "JSON";
136 public static final int Html_FILE = 3;
137 public static final String Html_FILE_EXT = "html";
138 public static final String Html_FILE_DESC = "HTML";