package jalview.jsdev; import jalview.datamodel.AlignmentI; import jalview.io.AlignFile; import jalview.io.FileParse; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.net.URL; import javajs.J2SIgnoreImport; /** * A class to open files via reflection so that their classes are only loaded as necessary * * note: not all file openers have corresponding String inFile and FileParse * source options. Why not? * * note: Pileupfile does not have a capital "F"; the method below requires that, * so if that file name gets changed, so too does the reflection code here. * * @author Bob Hanson * */ @J2SIgnoreImport({java.io.FileReader.class}) abstract public class GenericFileAdapter extends AlignFile { /** * inFileOrSource class type * * @param inFileOrSource * type will determine constructor -- [], [AlignmentI], * [inFile,type], or [source] * @param type * @param fileName * @return */ public static AlignFile getFile(String fileType, Object... params) { Class cl = null; try { cl = Class.forName("jalview.io." + fileType); } catch (ClassNotFoundException e) { System.err.println("did not find file jalview.io." + fileType); return null; } Constructor m; Throwable ex = null; try { switch (params.length) { case 0: return (AlignFile) cl.newInstance(); case 1: m = (params[0] instanceof FileParse ? cl .getConstructor(FileParse.class) : cl .getConstructor(AlignmentI.class)); break; case 2: m = cl.getConstructor(String.class, String.class); break; default: return null; } return (AlignFile) m.newInstance(params); } catch (InstantiationException e) { ex = e; } catch (IllegalAccessException e) { ex = e; } catch (NoSuchMethodException e) { ex = e; } catch (SecurityException e) { ex = e; } catch (IllegalArgumentException e) { ex = e; } catch (InvocationTargetException e) { ex = e; } if (ex != null) { System.err.println("Error in GenericFileAdapter: " + ex); /** * @j2sNative * * alert(ex) * */ { ex.printStackTrace(); } } return null; } /** * Determines whether or not we have a JavaScript applet. * * @return */ public static boolean isJS() { /** * @j2sNative * * return true; * */ { return false; } } /** * opens a file for line-oriented reading via File() or URL() * * @param fileName * @param forceURL * @return * @throws IOException */ public static BufferedReader getReader(String fileName, boolean forceURL) throws IOException { if (!forceURL && !isJS()) return new BufferedReader(new FileReader(fileName)); if (fileName.indexOf("//") < 0) fileName = "file://" + fileName; return new BufferedReader(new InputStreamReader(new URL(fileName).openStream())); } public final static String TCOFFEE_SCORE = "TCoffeeScore"; public static final int Phylip_FILE = 1; public static final String Phylip_FILE_EXT = "phy"; public static final String Phylip_FILE_DESC = "PHYLIP"; public static final int JSON_FILE = 2; public static final String JSON_FILE_EXT = "json"; public static final String JSON_FILE_DESC = "JSON"; public static final int Html_FILE = 3; public static final String Html_FILE_EXT = "html"; public static final String Html_FILE_DESC = "HTML"; }