d19a2030a2982ba1674898d1a740d6dbb06101a3
[jalview.git] / src / jalview / io / HtmlFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21
22 package jalview.io;
23
24 import jalview.api.ComplexAlignFile;
25 import jalview.api.FeatureSettingsModelI;
26 import jalview.api.FeaturesDisplayedI;
27 import jalview.datamodel.ColumnSelection;
28 import jalview.datamodel.SequenceI;
29
30 import java.io.BufferedReader;
31 import java.io.File;
32 import java.io.IOException;
33 import java.io.InputStreamReader;
34 import java.io.StringReader;
35 import java.net.URL;
36 import java.util.Objects;
37
38 import org.jsoup.Jsoup;
39 import org.jsoup.nodes.Document;
40 import org.jsoup.nodes.Element;
41
42 public class HtmlFile extends AlignFile implements ComplexAlignFile
43 {
44   public static final String FILE_EXT = "html";
45
46   public static final String FILE_DESC = "HTML";
47
48   private String globalColourScheme;
49
50   private boolean showSeqFeatures;
51
52   private ColumnSelection columnSelection;
53
54   private SequenceI[] hiddenSequences;
55
56   private FeaturesDisplayedI displayedFeatures;
57
58   public HtmlFile()
59   {
60     super();
61   }
62
63   public HtmlFile(FileParse source) throws IOException
64   {
65     super(source);
66   }
67
68   public HtmlFile(String inFile, String type) throws IOException
69   {
70     super(inFile, type);
71   }
72
73   @Override
74   public void parse() throws IOException
75   {
76     Element content = null;
77     Document doc = null;
78     try
79     {
80       StringBuilder htmlData = new StringBuilder();
81       String currentLine;
82       while ((currentLine = nextLine()) != null)
83       {
84         htmlData.append(currentLine);
85       }
86       doc = Jsoup.parse(htmlData.toString());
87     } catch (OutOfMemoryError oom)
88     {
89       errormessage = "Not enough memory to process HTML document";
90       throw new IOException(errormessage);
91     }
92
93     try
94     {
95       boolean contentFromDiv = true;
96       // search for BioJSON data in div element with id seqData
97       content = doc.select("div[id=seqData]").first();
98       if (content == null)
99       {
100         contentFromDiv = false;
101         // search for BioJSON data in input element with id seqData
102         content = doc.getElementById("seqData");
103       }
104
105       if (content == null)
106       {
107         errormessage = "The html document is not embedded with BioJSON data";
108         throw new IOException(errormessage);
109       }
110       JSONFile jsonFile = new JSONFile().parse(new StringReader(
111               contentFromDiv ? content.text() : content.val()));
112       this.seqs = jsonFile.getSeqs();
113       this.seqGroups = jsonFile.getSeqGroups();
114       this.annotations = jsonFile.getAnnotations();
115       this.showSeqFeatures = jsonFile.isShowSeqFeatures();
116       this.globalColourScheme = jsonFile.getGlobalColourScheme();
117       this.hiddenSequences = jsonFile.getHiddenSequences();
118       this.columnSelection = jsonFile.getColumnSelection();
119       this.displayedFeatures = jsonFile.getDisplayedFeatures();
120     } catch (Exception e)
121     {
122       throw e;
123     }
124   }
125
126   @Override
127   public String print()
128   {
129     throw new UnsupportedOperationException(
130             "Print method of HtmlFile is not supported!");
131   }
132
133   @Override
134   public boolean isShowSeqFeatures()
135   {
136     return showSeqFeatures;
137   }
138
139   public void setShowSeqFeatures(boolean showSeqFeatures)
140   {
141     this.showSeqFeatures = showSeqFeatures;
142   }
143
144   @Override
145   public String getGlobalColourScheme()
146   {
147     return globalColourScheme;
148   }
149
150   public void setColourScheme(String globalColourScheme)
151   {
152     this.globalColourScheme = globalColourScheme;
153   }
154
155   @Override
156   public ColumnSelection getColumnSelection()
157   {
158     return columnSelection;
159   }
160
161   public void setColumnSelection(ColumnSelection columnSelection)
162   {
163     this.columnSelection = columnSelection;
164   }
165
166   @Override
167   public SequenceI[] getHiddenSequences()
168   {
169     return hiddenSequences;
170   }
171
172   public void setHiddenSequences(SequenceI[] hiddenSequences)
173   {
174     this.hiddenSequences = hiddenSequences;
175   }
176
177   @Override
178   public FeaturesDisplayedI getDisplayedFeatures()
179   {
180     return displayedFeatures;
181   }
182
183   /**
184    * Returns a descriptor for suitable feature display settings with
185    * <ul>
186    * <li>ResNums or insertions features visible</li>
187    * <li>insertions features coloured red</li>
188    * <li>ResNum features coloured by label</li>
189    * <li>Insertions displayed above (on top of) ResNums</li>
190    * </ul>
191    */
192   @Override
193   public FeatureSettingsModelI getFeatureColourScheme()
194   {
195     return new PDBFeatureSettings();
196   }
197
198   /**
199    * Read a template file content as string
200    * 
201    * @param file
202    *          - the file to be read
203    * @return File content as String
204    * @throws IOException
205    */
206   public static String readFileAsString(File file) throws IOException
207   {
208     InputStreamReader isReader = null;
209     BufferedReader buffReader = null;
210     StringBuilder sb = new StringBuilder();
211     Objects.requireNonNull(file, "File must not be null!");
212     @SuppressWarnings("deprecation")
213     URL url = file.toURL();
214     if (url != null)
215     {
216       try
217       {
218         isReader = new InputStreamReader(url.openStream());
219         buffReader = new BufferedReader(isReader);
220         String line;
221         String lineSeparator = System.getProperty("line.separator");
222         while ((line = buffReader.readLine()) != null)
223         {
224           sb.append(line).append(lineSeparator);
225         }
226   
227       } catch (Exception ex)
228       {
229         ex.printStackTrace();
230       } finally
231       {
232         if (isReader != null)
233         {
234           isReader.close();
235         }
236   
237         if (buffReader != null)
238         {
239           buffReader.close();
240         }
241       }
242     }
243     return sb.toString();
244   }
245
246 }