JAL-1807 - Bob's last(?) before leaving Dundee -- adds fast file loading
[jalviewjs.git] / src / jalview / jsdev / GenericFileAdapter.java
1 package jalview.jsdev;
2
3 import jalview.datamodel.AlignmentI;
4 import jalview.io.AlignFile;
5 import jalview.io.FileParse;
6
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;
13 import java.net.URL;
14
15 import javajs.J2SIgnoreImport;
16 /**
17  * A class to open files via reflection so that their classes are only loaded as necessary
18  * 
19  * note: not all file openers have corresponding String inFile and FileParse
20  * source options. Why not?
21  * 
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.
24  * 
25  * @author Bob Hanson
26  * 
27  */
28
29 @J2SIgnoreImport({java.io.FileReader.class})
30 abstract public class GenericFileAdapter extends AlignFile {
31
32         /**
33          * inFileOrSource class type
34          * 
35          * @param inFileOrSource
36          *          type will determine constructor -- [], [AlignmentI],
37          *          [inFile,type], or [source]
38          * @param type
39          * @param fileName
40          * @return
41          */
42         public static AlignFile getFile(String fileType, Object... params) {
43                 Class<?> cl = null;
44                 try {
45                         cl = Class.forName("jalview.io." + fileType);
46                 } catch (ClassNotFoundException e) {
47                         System.err.println("did not find file jalview.io." + fileType);
48                         return null;
49                 }
50                 Constructor<?> m;
51                 Throwable ex = null;
52                 try {
53                         switch (params.length) {
54                         case 0:
55                                 return (AlignFile) cl.newInstance();
56                         case 1:
57                                 m = (params[0] instanceof FileParse ? cl
58                                                 .getConstructor(FileParse.class) : cl
59                                                 .getConstructor(AlignmentI.class));
60                                 break;
61                         case 2:
62                                 m = cl.getConstructor(String.class, String.class);
63                                 break;
64                         default:
65                                 return null;
66                         }
67                         return (AlignFile) m.newInstance(params);
68                 } catch (InstantiationException e) {
69                         ex = e;
70                 } catch (IllegalAccessException e) {
71                         ex = e;
72                 } catch (NoSuchMethodException e) {
73                         ex = e;
74                 } catch (SecurityException e) {
75                         ex = e;
76                 } catch (IllegalArgumentException e) {
77                         ex = e;
78                 } catch (InvocationTargetException e) {
79                         ex = e;
80                 }
81                 if (ex != null) {
82                         System.err.println("Error in GenericFileAdapter: " + ex);
83                         /**
84                          * @j2sNative
85                          * 
86                          *            alert(ex)
87                          * 
88                          */
89                         {
90                                 ex.printStackTrace();
91                         }
92                 }
93                 return null;
94         }
95
96         /**
97          * Determines whether or not we have a JavaScript applet.
98          * 
99          * @return
100          */
101         public static boolean isJS() {
102         /**
103          * @j2sNative 
104          * 
105          * return true;
106          * 
107          */
108         {
109                 return false;
110         } 
111         }
112
113         /**
114          * opens a file for line-oriented reading via File() or URL()
115          * 
116          * @param fileName
117          * @param forceURL
118          * @return
119          * @throws IOException
120          */
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()));
127         }
128
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";
139         
140
141 }