inprogress
[jalview.git] / forester / java / src / org / forester / phylogeny / factories / ParserBasedPhylogenyFactory.java
1 // $Id:
2 // $
3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
5 //
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
8 // All rights reserved
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 //
24 // Contact: phylosoft @ gmail . com
25 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26
27 package org.forester.phylogeny.factories;
28
29 import java.io.IOException;
30
31 import org.forester.io.parsers.PhylogenyParser;
32 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
33 import org.forester.phylogeny.Phylogeny;
34 import org.forester.util.ForesterUtil;
35
36 public class ParserBasedPhylogenyFactory implements PhylogenyFactory {
37
38     private final static PhylogenyFactory _instance;
39     static {
40         try {
41             _instance = new ParserBasedPhylogenyFactory();
42         }
43         catch ( final Throwable e ) {
44             throw new RuntimeException( e.getMessage() );
45         }
46     }
47
48     private ParserBasedPhylogenyFactory() {
49         // Private constructor.
50     }
51
52     @Override
53     public Object clone() throws CloneNotSupportedException {
54         throw new CloneNotSupportedException();
55     }
56
57     @Override
58     public synchronized Phylogeny[] create( final Object source, final Object parser ) throws IOException {
59         if ( !( parser instanceof PhylogenyParser ) ) {
60             throw new IllegalArgumentException( "attempt to use object of type other than PhylogenyParser as creator for ParserBasedPhylogenyFactory" );
61         }
62         final PhylogenyParser my_parser = ( PhylogenyParser ) parser;
63         my_parser.setSource( source );
64         return my_parser.parse();
65     }
66
67     public synchronized Phylogeny[] create( final Object source, final Object parser, final String schema_location )
68             throws IOException {
69         if ( !( parser instanceof PhylogenyParser ) ) {
70             throw new IllegalArgumentException( "attempt to use object of type other than PhylogenyParser as creator for ParserBasedPhylogenyFactory." );
71         }
72         if ( !( parser instanceof PhyloXmlParser ) ) {
73             throw new IllegalArgumentException( "attempt to use schema location with other than phyloXML parser" );
74         }
75         final PhyloXmlParser xml_parser = ( PhyloXmlParser ) parser;
76         if ( !ForesterUtil.isEmpty( schema_location ) ) {
77             xml_parser.setValidateAgainstSchema( schema_location );
78         }
79         xml_parser.setSource( source );
80         return xml_parser.parse();
81     }
82
83     public static PhylogenyFactory getInstance() {
84         return _instance;
85     }
86 }