2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
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;
30 import java.io.BufferedReader;
32 import java.io.IOException;
33 import java.io.InputStreamReader;
34 import java.io.StringReader;
36 import java.util.Objects;
38 import org.jsoup.Jsoup;
39 import org.jsoup.nodes.Document;
40 import org.jsoup.nodes.Element;
42 public class HtmlFile extends AlignFile implements ComplexAlignFile
44 public static final String FILE_EXT = "html";
46 public static final String FILE_DESC = "HTML";
48 private String globalColourScheme;
50 private boolean showSeqFeatures;
52 private ColumnSelection columnSelection;
54 private SequenceI[] hiddenSequences;
56 private FeaturesDisplayedI displayedFeatures;
63 public HtmlFile(FileParse source) throws IOException
68 public HtmlFile(String inFile, String type) throws IOException
74 public void parse() throws IOException
76 Element content = null;
80 StringBuilder htmlData = new StringBuilder();
82 while ((currentLine = nextLine()) != null)
84 htmlData.append(currentLine);
86 doc = Jsoup.parse(htmlData.toString());
87 } catch (OutOfMemoryError oom)
89 errormessage = "Not enough memory to process HTML document";
90 throw new IOException(errormessage);
95 boolean contentFromDiv = true;
96 // search for BioJSON data in div element with id seqData
97 content = doc.select("div[id=seqData]").first();
100 contentFromDiv = false;
101 // search for BioJSON data in input element with id seqData
102 content = doc.getElementById("seqData");
107 errormessage = "The html document is not embedded with BioJSON data";
108 throw new IOException(errormessage);
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)
127 public String print()
129 throw new UnsupportedOperationException(
130 "Print method of HtmlFile is not supported!");
134 public boolean isShowSeqFeatures()
136 return showSeqFeatures;
139 public void setShowSeqFeatures(boolean showSeqFeatures)
141 this.showSeqFeatures = showSeqFeatures;
145 public String getGlobalColourScheme()
147 return globalColourScheme;
150 public void setColourScheme(String globalColourScheme)
152 this.globalColourScheme = globalColourScheme;
156 public ColumnSelection getColumnSelection()
158 return columnSelection;
161 public void setColumnSelection(ColumnSelection columnSelection)
163 this.columnSelection = columnSelection;
167 public SequenceI[] getHiddenSequences()
169 return hiddenSequences;
172 public void setHiddenSequences(SequenceI[] hiddenSequences)
174 this.hiddenSequences = hiddenSequences;
178 public FeaturesDisplayedI getDisplayedFeatures()
180 return displayedFeatures;
184 * Returns a descriptor for suitable feature display settings with
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>
193 public FeatureSettingsModelI getFeatureColourScheme()
195 return new PDBFeatureSettings();
199 * Read a template file content as string
202 * - the file to be read
203 * @return File content as String
204 * @throws IOException
206 public static String readFileAsString(File file) throws IOException
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();
218 isReader = new InputStreamReader(url.openStream());
219 buffReader = new BufferedReader(isReader);
221 String lineSeparator = System.getProperty("line.separator");
222 while ((line = buffReader.readLine()) != null)
224 sb.append(line).append(lineSeparator);
227 } catch (Exception ex)
229 ex.printStackTrace();
232 if (isReader != null)
237 if (buffReader != null)
243 return sb.toString();