06c271740a128fce710dd0ddb749d8291ec8d70a
[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.FeaturesDisplayedI;
26 import jalview.datamodel.ColumnSelection;
27 import jalview.datamodel.SequenceI;
28 import jalview.schemes.ColourSchemeI;
29
30 import java.io.IOException;
31 import java.io.StringReader;
32
33 import org.jsoup.Jsoup;
34 import org.jsoup.nodes.Document;
35 import org.jsoup.nodes.Element;
36
37 public class HtmlFile extends AlignFile implements ComplexAlignFile
38 {
39   public static final String FILE_EXT = "html";
40
41   public static final String FILE_DESC = "HTML";
42
43   private ColourSchemeI colourScheme;
44
45   private boolean showSeqFeatures;
46
47   private ColumnSelection columnSelection;
48
49   private SequenceI[] hiddenSequences;
50
51   private FeaturesDisplayedI displayedFeatures;
52
53   public HtmlFile()
54   {
55     super();
56   }
57
58   public HtmlFile(FileParse source) throws IOException
59   {
60     super(source);
61   }
62
63   public HtmlFile(String inFile, String type) throws IOException
64   {
65     super(inFile, type);
66   }
67
68   @Override
69   public void parse() throws IOException
70   {
71     Element content = null;
72     Document doc = null;
73     try
74     {
75       StringBuilder htmlData = new StringBuilder();
76       String currentLine;
77       while ((currentLine = nextLine()) != null)
78       {
79         htmlData.append(currentLine);
80       }
81       doc = Jsoup.parse(htmlData.toString());
82     } catch (OutOfMemoryError oom)
83     {
84       errormessage = "Not enough memory to process HTML document";
85       throw new IOException(errormessage);
86     }
87
88     try
89     {
90       boolean contentFromDiv = true;
91       // search for BioJSON data in div element with id seqData
92       content = doc.select("div[id=seqData]").first();
93       if (content == null)
94       {
95         contentFromDiv = false;
96         // search for BioJSON data in input element with id seqData
97         content = doc.getElementById("seqData");
98       }
99
100       if (content == null)
101       {
102         errormessage = "The html document is not embedded with BioJSON data";
103         throw new IOException(errormessage);
104       }
105       JSONFile jsonFile = new JSONFile().parse(new StringReader(
106               contentFromDiv ? content.text() : content.val()));
107       this.seqs = jsonFile.getSeqs();
108       this.seqGroups = jsonFile.getSeqGroups();
109       this.annotations = jsonFile.getAnnotations();
110       this.showSeqFeatures = jsonFile.isShowSeqFeatures();
111       this.colourScheme = jsonFile.getColourScheme();
112       this.hiddenSequences = jsonFile.getHiddenSequences();
113       this.columnSelection = jsonFile.getColumnSelection();
114       this.displayedFeatures = jsonFile.getDisplayedFeatures();
115     } catch (Exception e)
116     {
117       throw e;
118     }
119   }
120
121   @Override
122   public String print()
123   {
124     throw new UnsupportedOperationException(
125             "Print method of HtmlFile is not supported!");
126   }
127
128   public boolean isShowSeqFeatures()
129   {
130     return showSeqFeatures;
131   }
132
133   public void setShowSeqFeatures(boolean showSeqFeatures)
134   {
135     this.showSeqFeatures = showSeqFeatures;
136   }
137
138   public ColourSchemeI getColourScheme()
139   {
140     return colourScheme;
141   }
142
143   public void setColourScheme(ColourSchemeI colourScheme)
144   {
145     this.colourScheme = colourScheme;
146   }
147
148   public ColumnSelection getColumnSelection()
149   {
150     return columnSelection;
151   }
152
153   public void setColumnSelection(ColumnSelection columnSelection)
154   {
155     this.columnSelection = columnSelection;
156   }
157
158   public SequenceI[] getHiddenSequences()
159   {
160     return hiddenSequences;
161   }
162
163   public void setHiddenSequences(SequenceI[] hiddenSequences)
164   {
165     this.hiddenSequences = hiddenSequences;
166   }
167
168   @Override
169   public FeaturesDisplayedI getDisplayedFeatures()
170   {
171     return displayedFeatures;
172   }
173
174 }