bc5c21941ca285ae4351824f82c0447619fb61fc
[jalview.git] / src / jalview / io / HtmlFile.java
1 package jalview.io;
2
3 import jalview.datamodel.Sequence;
4
5 import java.io.IOException;
6 import java.util.Iterator;
7
8 import org.json.simple.JSONArray;
9 import org.json.simple.JSONObject;
10 import org.json.simple.parser.JSONParser;
11 import org.jsoup.Jsoup;
12 import org.jsoup.nodes.Document;
13 import org.jsoup.nodes.Element;
14
15 public class HtmlFile extends AlignFile {
16         public static final String FILE_EXT = "html";
17
18         public static final String FILE_DESC = "HTML";
19
20         public HtmlFile() {
21                 super();
22         }
23
24         public HtmlFile(FileParse source) throws IOException {
25                 super(source);
26         }
27
28         public HtmlFile(String inFile, String type) throws IOException {
29                 super(inFile, type);
30         }
31
32         @SuppressWarnings("unchecked")
33         @Override
34         public void parse() throws IOException {
35                 try {
36                         StringBuilder htmlData = new StringBuilder();
37                         String currentLine;
38                         while ((currentLine = nextLine()) != null) {
39                                 htmlData.append(currentLine);
40                         }
41
42                         Document doc = Jsoup.parse(htmlData.toString());
43                         Element content = doc.getElementById("seqData");
44                         
45                         String alignmentJsonString = "{\"seqs\":" + content.val() + "}";
46                         JSONParser jsonParser = new JSONParser();
47                         JSONObject alignmentJsonObj = (JSONObject) jsonParser.parse(alignmentJsonString);
48                         JSONArray seqJsonArray = (JSONArray) alignmentJsonObj.get("seqs");
49
50                         for (Iterator<JSONObject> sequenceIter = seqJsonArray.iterator(); sequenceIter.hasNext();) {
51                                 JSONObject sequence = sequenceIter.next();
52                                 System.out.println(sequence.get("id").toString() + " " + sequence.get("name"));
53                                 String sequcenceString = sequence.get("seq").toString();
54                                 Sequence seq = new Sequence(sequence.get("name").toString(), sequcenceString, 0, sequcenceString.length());
55                                 seqs.add(seq);
56
57                         }
58                 } catch (Exception e) {
59                         e.printStackTrace();
60                 }
61         }
62
63         @Override
64         public String print() {
65                 throw new UnsupportedOperationException("Print method of HtmlFile not yet supported!");
66         }
67
68 }