JAL-1953 2.11.2 with Archeopteryx!
[jalview.git] / src / jalview / ext / forester / io / ForesterParser.java
diff --git a/src/jalview/ext/forester/io/ForesterParser.java b/src/jalview/ext/forester/io/ForesterParser.java
new file mode 100644 (file)
index 0000000..b0c2bdb
--- /dev/null
@@ -0,0 +1,90 @@
+package jalview.ext.forester.io;
+
+import jalview.ext.archaeopteryx.AptxInit;
+import jalview.ext.archaeopteryx.Tree;
+import jalview.ext.treeviewer.TreeI;
+import jalview.ext.treeviewer.TreeParserI;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.forester.io.parsers.PhylogenyParser;
+import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
+import org.forester.io.parsers.phyloxml.PhyloXmlParser;
+import org.forester.io.parsers.util.PhylogenyParserException;
+import org.forester.phylogeny.Phylogeny;
+
+public class ForesterParser
+        implements TreeParserI
+{
+  private final PhylogenyParser parser;
+
+  private TreeI[] parsedTrees;
+
+  protected ForesterParser(PhylogenyParser foresterParser, File file)
+          throws PhylogenyParserException, IOException
+  {
+    parser = foresterParser;
+    parser.setSource(file);
+  }
+
+  public static ForesterParser createNexusParser(File file)
+          throws PhylogenyParserException, IOException
+  {
+    NexusPhylogeniesParser nxParser = new NexusPhylogeniesParser();
+    nxParser.setReplaceUnderscores(
+            AptxInit.APTX_CONFIG.isReplaceUnderscoresInNhParsing());
+    nxParser.setIgnoreQuotes(false);
+    return new ForesterParser(nxParser, file);
+  }
+
+  public static ForesterParser createPhyloXmlParser(File file)
+          throws PhylogenyParserException, IOException
+  {
+    if (AptxInit.APTX_CONFIG.isValidatePhyloXmlAgainstSchema())
+    {
+    return new ForesterParser(
+              PhyloXmlParser.createPhyloXmlParserXsdValidating(), file);
+    }
+    else
+    {
+      return new ForesterParser(PhyloXmlParser.createPhyloXmlParser(),
+              file);
+    }
+  }
+
+  // ParserBasedPhylogenyFactory.getInstance().create(foresterParser, source)
+  @Override
+  public TreeI[] parse() throws IOException
+  {
+    Phylogeny[] foresterTrees = parser.parse();
+    parsedTrees = new TreeI[foresterTrees.length];
+
+    for (int i = 0; i < foresterTrees.length; i++)
+    {
+      parsedTrees[i] = new Tree(foresterTrees[i]);
+    }
+    return parsedTrees;
+
+  }
+
+  @Override
+  public void setSource(Object source) throws IOException
+  {
+    parser.setSource(source);
+
+  }
+
+  @Override
+  public String getName()
+  {
+    return parser.getName();
+  }
+
+
+  @Override
+  public TreeI[] getParsedTrees()
+  {
+    return parsedTrees;
+  }
+}
\ No newline at end of file