3 import jalview.exceptions.NoFileSelectedException;
4 import jalview.ftp.FtpClient;
5 import jalview.gui.AlignViewport;
6 import jalview.gui.AlignmentPanel;
7 import jalview.gui.FeatureRenderer;
8 import jalview.util.MessageManager;
10 import java.io.BufferedReader;
12 import java.io.IOException;
13 import java.io.InputStreamReader;
14 import java.io.PrintWriter;
15 import java.net.URISyntaxException;
17 import java.util.Objects;
18 import java.util.TreeMap;
20 import org.apache.commons.net.ftp.FTP;
21 import org.apache.commons.net.ftp.FTPClient;
22 import org.apache.commons.net.ftp.FTPFile;
25 public class BioJsHTMLOutput
27 private AlignViewport av;
29 private static File currentBJSTemplateFile;
31 private static TreeMap<String, File> bioJsMSAVersions;
33 public static final String BJS_TEMPLATES_LOCAL_DIRECTORY = jalview.bin.Cache
34 .getDefault("biojs_template_directory", "/biojs_templates/");
36 public static final String BJS_FTP_USER = jalview.bin.Cache.getDefault(
37 "biojs_ftp_user", "test");
39 public static final String BJS_FTP_PWD = jalview.bin.Cache.getDefault(
40 "biojs_ftp_pwd", "test");
42 public static final String BJS_FTP_PORT = jalview.bin.Cache.getDefault(
43 "biojs_ftp_port", "22");
45 public static final String BJS_FTP_SERVER = jalview.bin.Cache.getDefault(
46 "biojs_ftp_server", "localhost");
48 public BioJsHTMLOutput(AlignmentPanel ap,
56 av.setFeatureRenderer(new FeatureRenderer(ap));
61 public void exportJalviewAlignmentAsBioJsHtmlFile()
65 String outputFile = getOutputFile();
66 String jalviewAlignmentJson = JSONFile.getJSONData(av);
67 String bioJSTemplateString = getBioJsTemplateAsString();
68 String generatedBioJsWithJalviewAlignmentAsJson = bioJSTemplateString
70 "#sequenceData#", jalviewAlignmentJson)
73 PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(
75 out.print(generatedBioJsWithJalviewAlignmentAsJson);
78 jalview.util.BrowserLauncher.openURL("file:///" + outputFile);
79 } catch (NoFileSelectedException ex)
81 // do noting if no file was selected
88 public String getOutputFile() throws NoFileSelectedException
90 String selectedFile = null;
91 JalviewFileChooser jvFileChooser = new JalviewFileChooser(
92 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
93 { "html" }, new String[]
94 { "HTML files" }, "HTML files");
95 jvFileChooser.setFileView(new JalviewFileView());
97 // TODO uncomment when supported by MassageManager
98 jvFileChooser.setDialogTitle(MessageManager
99 .getString("label.save_as_biojs_html"));
100 jvFileChooser.setDialogTitle("save as BioJs HTML");
101 jvFileChooser.setToolTipText(MessageManager.getString("action.save"));
103 int fileChooserOpt = jvFileChooser.showSaveDialog(null);
104 if (fileChooserOpt == JalviewFileChooser.APPROVE_OPTION)
106 jalview.bin.Cache.setProperty("LAST_DIRECTORY", jvFileChooser
107 .getSelectedFile().getParent());
108 selectedFile = jvFileChooser.getSelectedFile().getPath();
112 throw new NoFileSelectedException("No file was selected.");
118 public static String getBioJsTemplateAsString()
121 InputStreamReader isReader = null;
122 BufferedReader buffReader = null;
123 StringBuilder sb = new StringBuilder();
124 Objects.requireNonNull(getCurrentBJSTemplateFile(),
125 "BioJsTemplate File not initialized!");
126 @SuppressWarnings("deprecation")
127 URL url = getCurrentBJSTemplateFile().toURL();
132 isReader = new InputStreamReader(url.openStream());
133 buffReader = new BufferedReader(isReader);
135 String lineSeparator = System.getProperty("line.separator");
136 while ((line = buffReader.readLine()) != null)
138 sb.append(line).append(lineSeparator);
141 } catch (Exception ex)
143 ex.printStackTrace();
146 if (isReader != null)
151 if (buffReader != null)
157 return sb.toString();
160 public TreeMap<String, File> updateBioJSVersionsInfo(String dirName)
161 throws URISyntaxException
163 URL url = getClass().getResource(dirName);
164 File directory = new File(url.toURI());
165 Objects.requireNonNull(dirName, "dirName MUST not be null!");
166 Objects.requireNonNull(directory, "directory MUST not be null!");
167 TreeMap<String, File> versionFileMap = new TreeMap<String, File>();
169 for (File file : directory.listFiles())
173 String fileName = file.getName().substring(0,
174 file.getName().lastIndexOf("."));
175 String fileMeta[] = fileName.split("_");
176 if (fileMeta.length > 2)
178 setCurrentBJSTemplateFile(file);
179 versionFileMap.put(fileMeta[2], file);
181 else if (fileMeta.length > 1)
183 versionFileMap.put(fileMeta[1], file);
187 if (getCurrentBJSTemplateFile() == null && versionFileMap.size() > 0)
189 setCurrentBJSTemplateFile(versionFileMap.lastEntry().getValue());
191 return versionFileMap;
194 public void updateBioJS()
196 TreeMap<String, File> versionLists = null;
199 // downlaodNewBioJsTemplates(BJS_TEMPLATES_LOCAL_DIRECTORY);
200 versionLists = updateBioJSVersionsInfo(BJS_TEMPLATES_LOCAL_DIRECTORY);
201 setBioJsMSAVersions(versionLists);
202 } catch (URISyntaxException e)
208 public void downlaodNewBioJsTemplates(String localDirectory)
210 FTPClient client = FtpClient.getFtpClient(BJS_FTP_SERVER);
211 if (FtpClient.authenticateUser(client, BJS_FTP_USER, BJS_FTP_PWD))
213 client.enterLocalPassiveMode();
216 client.setFileType(FTP.BINARY_FILE_TYPE);
217 for (FTPFile fFile : client.listFiles())
219 String localFileName = BJS_TEMPLATES_LOCAL_DIRECTORY
221 String remoteFileName = fFile.getName();
222 FtpClient.downloadFile(client, remoteFileName, localFileName);
224 } catch (IOException e)
231 // public static void main(String[] args) throws IOException
233 // Document doc = Jsoup.connect("http://howto.unixdev.net").get();
234 // for (Element file : doc.select("td.right td a"))
236 // System.out.println(file.attr("href"));
240 public static File getCurrentBJSTemplateFile()
242 return currentBJSTemplateFile;
245 public static void setCurrentBJSTemplateFile(File currentBJSTemplateFile)
247 BioJsHTMLOutput.currentBJSTemplateFile = currentBJSTemplateFile;
250 public static TreeMap<String, File> getBioJsMSAVersions()
252 return bioJsMSAVersions;
255 public static void setBioJsMSAVersions(
256 TreeMap<String, File> bioJsMSAVersions)
258 BioJsHTMLOutput.bioJsMSAVersions = bioJsMSAVersions;