JAL-2089 patch broken merge to master for Release 2.10.0b1
[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.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 String globalColourScheme;
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.globalColourScheme = jsonFile.getGlobalColourScheme();
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   @Override
129   public boolean isShowSeqFeatures()
130   {
131     return showSeqFeatures;
132   }
133
134   public void setShowSeqFeatures(boolean showSeqFeatures)
135   {
136     this.showSeqFeatures = showSeqFeatures;
137   }
138
139   @Override
140   public String getGlobalColourScheme()
141   {
142     return globalColourScheme;
143   }
144
145   public void setColourScheme(String globalColourScheme)
146   {
147     this.globalColourScheme = globalColourScheme;
148   }
149
150   @Override
151   public ColumnSelection getColumnSelection()
152   {
153     return columnSelection;
154   }
155
156   public void setColumnSelection(ColumnSelection columnSelection)
157   {
158     this.columnSelection = columnSelection;
159   }
160
161   @Override
162   public SequenceI[] getHiddenSequences()
163   {
164     return hiddenSequences;
165   }
166
167   public void setHiddenSequences(SequenceI[] hiddenSequences)
168   {
169     this.hiddenSequences = hiddenSequences;
170   }
171
172   @Override
173   public FeaturesDisplayedI getDisplayedFeatures()
174   {
175     return displayedFeatures;
176   }
177
178   /**
179    * Returns a descriptor for suitable feature display settings with
180    * <ul>
181    * <li>ResNums or insertions features visible</li>
182    * <li>insertions features coloured red</li>
183    * <li>ResNum features coloured by label</li>
184    * <li>Insertions displayed above (on top of) ResNums</li>
185    * </ul>
186    */
187   @Override
188   public FeatureSettingsModelI getFeatureColourScheme()
189   {
190     return new PDBFeatureSettings();
191   }
192
193 }