JAL-1784 minimise data duplication, and report out of memory errors and other kinds...
[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 java.io.IOException;
25 import java.io.StringReader;
26
27 import org.jsoup.Jsoup;
28 import org.jsoup.nodes.Document;
29 import org.jsoup.nodes.Element;
30
31 import jalview.api.ComplexAlignFile;
32 import jalview.datamodel.ColumnSelection;
33 import jalview.datamodel.SequenceI;
34 import jalview.schemes.ColourSchemeI;
35
36 public class HtmlFile extends AlignFile implements ComplexAlignFile
37 {
38   public static final String FILE_EXT = "html";
39
40   public static final String FILE_DESC = "HTML";
41
42   private ColourSchemeI colourScheme;
43
44   private boolean showSeqFeatures;
45
46   private ColumnSelection columnSelection;
47
48   private SequenceI[] hiddenSequences;
49
50   public HtmlFile()
51   {
52     super();
53   }
54
55   public HtmlFile(FileParse source) throws IOException
56   {
57     super(source);
58   }
59
60   public HtmlFile(String inFile, String type) throws IOException
61   {
62     super(inFile, type);
63   }
64
65   @Override
66   public void parse() throws IOException
67   {
68     try
69     {
70       Element content = null;
71       Document doc = null;
72       try {
73           StringBuilder htmlData = new StringBuilder();
74           String currentLine;
75           while ((currentLine = nextLine()) != null)
76           {
77             htmlData.append(currentLine);
78           }
79
80           doc = Jsoup.parse(htmlData.toString());
81       } catch (OutOfMemoryError oom) {
82           errormessage = "Not enough memory to process HTML document";
83           throw new IOException(errormessage);
84       }
85       content = doc.getElementById("seqData");
86       
87       JSONFile jsonFile = new JSONFile().parse(new StringReader(content.val()));
88       this.seqs = jsonFile.getSeqs();
89       this.seqGroups = jsonFile.getSeqGroups();
90       this.annotations = jsonFile.getAnnotations();
91       this.showSeqFeatures = jsonFile.isShowSeqFeatures();
92       this.colourScheme = jsonFile.getColourScheme();
93       this.hiddenSequences = jsonFile.getHiddenSequences();
94       this.columnSelection = jsonFile.getColumnSelection();
95     } catch (Exception e)
96     {
97       errormessage = "Failed to extract data from HTML document.";
98       throw new IOException("Unexpected exception whilst extracting JSon from HTML.",e);
99     }
100   }
101
102
103   @Override
104   public String print()
105   {
106     throw new UnsupportedOperationException(
107             "Print method of HtmlFile is not supported!");
108   }
109
110   public boolean isShowSeqFeatures()
111   {
112     return showSeqFeatures;
113   }
114
115   public void setShowSeqFeatures(boolean showSeqFeatures)
116   {
117     this.showSeqFeatures = showSeqFeatures;
118   }
119
120   public ColourSchemeI getColourScheme()
121   {
122     return colourScheme;
123   }
124
125   public void setColourScheme(ColourSchemeI colourScheme)
126   {
127     this.colourScheme = colourScheme;
128   }
129
130   public ColumnSelection getColumnSelection()
131   {
132     return columnSelection;
133   }
134
135   public void setColumnSelection(ColumnSelection columnSelection)
136   {
137     this.columnSelection = columnSelection;
138   }
139
140   public SequenceI[] getHiddenSequences()
141   {
142     return hiddenSequences;
143   }
144
145   public void setHiddenSequences(SequenceI[] hiddenSequences)
146   {
147     this.hiddenSequences = hiddenSequences;
148   }
149
150 }