JAL-2805 parser is now File based (instead of FileParse) and source is
[jalview.git] / src / jalview / ext / forester / io / ForesterParser.java
1 package jalview.ext.forester.io;
2
3 import jalview.ext.archaeopteryx.Tree;
4 import jalview.ext.treeviewer.TreeI;
5 import jalview.ext.treeviewer.TreeParserI;
6
7 import java.io.File;
8 import java.io.IOException;
9
10 import org.forester.io.parsers.PhylogenyParser;
11 import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
12 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
13 import org.forester.io.parsers.util.PhylogenyParserException;
14 import org.forester.phylogeny.Phylogeny;
15
16 public class ForesterParser
17         implements TreeParserI
18 {
19   private final PhylogenyParser parser;
20
21   private TreeI[] parsedTrees;
22
23   protected ForesterParser(PhylogenyParser foresterParser, File file)
24           throws PhylogenyParserException, IOException
25   {
26     parser = foresterParser;
27     parser.setSource(file);
28   }
29
30   public static ForesterParser createNexusParser(File file)
31           throws PhylogenyParserException, IOException
32   {
33     return new ForesterParser(new NexusPhylogeniesParser(), file);
34   }
35
36   public static ForesterParser createPhyloXmlParser(File file)
37           throws PhylogenyParserException, IOException
38   {
39     // support non-xsd validating?
40     return new ForesterParser(
41             PhyloXmlParser.createPhyloXmlParserXsdValidating(), file);
42   }
43
44   // ParserBasedPhylogenyFactory.getInstance().create(foresterParser, source)
45   @Override
46   public TreeI[] parse() throws IOException
47   {
48     Phylogeny[] foresterTrees = parser.parse();
49     parsedTrees = new TreeI[foresterTrees.length];
50
51     for (int i = 0; i < foresterTrees.length; i++)
52     {
53       parsedTrees[i] = new Tree(foresterTrees[i]);
54     }
55     return parsedTrees;
56
57   }
58
59   @Override
60   public void setSource(Object source) throws IOException
61   {
62     parser.setSource(source);
63
64   }
65
66   @Override
67   public String getName()
68   {
69     return parser.getName();
70   }
71
72
73   @Override
74   public TreeI[] getParsedTrees()
75   {
76     return parsedTrees;
77   }
78 }