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