9c159911ea93920f05a887ae6804bcb6875ae8ac
[jalview.git] / src / jalview / io / BioJsHTMLOutput.java
1 package jalview.io;
2
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;
9
10 import java.io.BufferedReader;
11 import java.io.File;
12 import java.io.IOException;
13 import java.io.InputStreamReader;
14 import java.io.PrintWriter;
15 import java.net.URISyntaxException;
16 import java.net.URL;
17 import java.util.Objects;
18 import java.util.TreeMap;
19
20 import org.apache.commons.net.ftp.FTP;
21 import org.apache.commons.net.ftp.FTPClient;
22 import org.apache.commons.net.ftp.FTPFile;
23
24
25 public class BioJsHTMLOutput
26 {
27   private AlignViewport av;
28
29   private static File currentBJSTemplateFile;
30
31   private static TreeMap<String, File> bioJsMSAVersions;
32
33   public static final String BJS_TEMPLATES_LOCAL_DIRECTORY = jalview.bin.Cache
34           .getDefault("biojs_template_directory", "/biojs_templates/");
35
36   public static final String BJS_FTP_USER = jalview.bin.Cache.getDefault(
37           "biojs_ftp_user", "test");
38
39   public static final String BJS_FTP_PWD = jalview.bin.Cache.getDefault(
40           "biojs_ftp_pwd", "test");
41
42   public static final String BJS_FTP_PORT = jalview.bin.Cache.getDefault(
43           "biojs_ftp_port", "22");
44
45   public static final String BJS_FTP_SERVER = jalview.bin.Cache.getDefault(
46           "biojs_ftp_server", "localhost");
47
48   public BioJsHTMLOutput(AlignmentPanel ap,
49           FeatureRenderer fr1)
50   {
51
52     if (ap != null)
53     {
54
55       this.av = ap.av;
56       av.setFeatureRenderer(new FeatureRenderer(ap));
57     }
58
59   }
60
61   public void exportJalviewAlignmentAsBioJsHtmlFile()
62   {
63     try
64     {
65       String outputFile = getOutputFile();
66       String jalviewAlignmentJson = JSONFile.getJSONData(av);
67       String bioJSTemplateString = getBioJsTemplateAsString();
68       String generatedBioJsWithJalviewAlignmentAsJson = bioJSTemplateString
69               .replaceAll(
70 "#sequenceData#", jalviewAlignmentJson)
71               .toString();
72
73       PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(
74               outputFile));
75       out.print(generatedBioJsWithJalviewAlignmentAsJson);
76       out.flush();
77       out.close();
78       jalview.util.BrowserLauncher.openURL("file:///" + outputFile);
79     } catch (NoFileSelectedException ex)
80     {
81       // do noting if no file was selected
82     } catch (Exception e)
83     {
84       e.printStackTrace();
85     }
86   }
87
88   public String getOutputFile() throws NoFileSelectedException
89   {
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());
96
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"));
102
103     int fileChooserOpt = jvFileChooser.showSaveDialog(null);
104     if (fileChooserOpt == JalviewFileChooser.APPROVE_OPTION)
105     {
106       jalview.bin.Cache.setProperty("LAST_DIRECTORY", jvFileChooser
107               .getSelectedFile().getParent());
108       selectedFile = jvFileChooser.getSelectedFile().getPath();
109     }
110     else
111     {
112       throw new NoFileSelectedException("No file was selected.");
113     }
114     return selectedFile;
115   }
116
117
118   public static String getBioJsTemplateAsString()
119           throws IOException
120   {
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();
128     if (url != null)
129     {
130       try
131       {
132         isReader = new InputStreamReader(url.openStream());
133         buffReader = new BufferedReader(isReader);
134         String line;
135         String lineSeparator = System.getProperty("line.separator");
136         while ((line = buffReader.readLine()) != null)
137         {
138           sb.append(line).append(lineSeparator);
139         }
140
141       } catch (Exception ex)
142       {
143         ex.printStackTrace();
144       } finally
145       {
146         if (isReader != null)
147         {
148           isReader.close();
149         }
150
151         if (buffReader != null)
152         {
153           buffReader.close();
154         }
155       }
156     }
157     return sb.toString();
158   }
159
160   public TreeMap<String, File> updateBioJSVersionsInfo(String dirName)
161           throws URISyntaxException
162   {
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>();
168
169     for (File file : directory.listFiles())
170     {
171       if (file.isFile())
172       {
173         String fileName = file.getName().substring(0,
174                 file.getName().lastIndexOf("."));
175         String fileMeta[] = fileName.split("_");
176         if (fileMeta.length > 2)
177         {
178           setCurrentBJSTemplateFile(file);
179           versionFileMap.put(fileMeta[2], file);
180         }
181         else if (fileMeta.length > 1)
182         {
183           versionFileMap.put(fileMeta[1], file);
184         }
185       }
186     }
187     if (getCurrentBJSTemplateFile() == null && versionFileMap.size() > 0)
188     {
189       setCurrentBJSTemplateFile(versionFileMap.lastEntry().getValue());
190     }
191     return versionFileMap;
192   }
193
194   public void updateBioJS()
195   {
196     TreeMap<String, File> versionLists = null;
197     try
198     {
199       // downlaodNewBioJsTemplates(BJS_TEMPLATES_LOCAL_DIRECTORY);
200       versionLists = updateBioJSVersionsInfo(BJS_TEMPLATES_LOCAL_DIRECTORY);
201       setBioJsMSAVersions(versionLists);
202     } catch (URISyntaxException e)
203     {
204       e.printStackTrace();
205     }
206   }
207
208   public void downlaodNewBioJsTemplates(String localDirectory)
209   {
210     FTPClient client = FtpClient.getFtpClient(BJS_FTP_SERVER);
211     if (FtpClient.authenticateUser(client, BJS_FTP_USER, BJS_FTP_PWD))
212     {
213       client.enterLocalPassiveMode();
214       try
215       {
216         client.setFileType(FTP.BINARY_FILE_TYPE);
217         for (FTPFile fFile : client.listFiles())
218         {
219           String localFileName = BJS_TEMPLATES_LOCAL_DIRECTORY
220                   + fFile.getName();
221           String remoteFileName = fFile.getName();
222           FtpClient.downloadFile(client, remoteFileName, localFileName);
223         }
224       } catch (IOException e)
225       {
226         e.printStackTrace();
227       }
228     }
229   }
230
231   // public static void main(String[] args) throws IOException
232   // {
233   // Document doc = Jsoup.connect("http://howto.unixdev.net").get();
234   // for (Element file : doc.select("td.right td a"))
235   // {
236   // System.out.println(file.attr("href"));
237   // }
238   // }
239
240   public static File getCurrentBJSTemplateFile()
241   {
242     return currentBJSTemplateFile;
243   }
244
245   public static void setCurrentBJSTemplateFile(File currentBJSTemplateFile)
246   {
247     BioJsHTMLOutput.currentBJSTemplateFile = currentBJSTemplateFile;
248   }
249
250   public static TreeMap<String, File> getBioJsMSAVersions()
251   {
252     return bioJsMSAVersions;
253   }
254
255   public static void setBioJsMSAVersions(
256           TreeMap<String, File> bioJsMSAVersions)
257   {
258     BioJsHTMLOutput.bioJsMSAVersions = bioJsMSAVersions;
259   }
260
261
262 }