18288520617ae9e2f21e54186269edcec0db10bd
[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: www.phylosoft.org/forester
26
27 package org.forester.phylogeny.factories;
28
29 import java.io.IOException;
30 import java.util.List;
31
32 import org.forester.io.parsers.PhylogenyParser;
33 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
34 import org.forester.phylogeny.Phylogeny;
35 import org.forester.util.ForesterUtil;
36
37 public class ParserBasedPhylogenyFactory extends BasicPhylogenyFactory {
38
39     private final static PhylogenyFactory _instance;
40     static {
41         try {
42             _instance = new ParserBasedPhylogenyFactory();
43         }
44         catch ( final Throwable e ) {
45             throw new RuntimeException( e.getMessage() );
46         }
47     }
48
49     private ParserBasedPhylogenyFactory() {
50         // Private constructor.
51     }
52
53     @Override
54     public Object clone() throws CloneNotSupportedException {
55         throw new CloneNotSupportedException();
56     }
57
58     @Override
59     public synchronized Phylogeny[] create( final Object source, final Object parser, final List<Object> parameters )
60             throws IOException {
61         if ( !( parser instanceof PhylogenyParser ) ) {
62             throw new IllegalArgumentException( "attempt to use object of type other than PhylogenyParser as creator for ParserBasedPhylogenyFactory" );
63         }
64         final PhylogenyParser my_parser = ( PhylogenyParser ) parser;
65         my_parser.setSource( source );
66         return my_parser.parse();
67     }
68
69     public synchronized Phylogeny[] create( final Object source,
70                                             final Object parser,
71                                             final String schema_location,
72                                             final List<Object> parameters ) throws IOException {
73         if ( !( parser instanceof PhylogenyParser ) ) {
74             throw new IllegalArgumentException( "attempt to use object of type other than PhylogenyParser as creator for ParserBasedPhylogenyFactory." );
75         }
76         if ( !( parser instanceof PhyloXmlParser ) ) {
77             throw new IllegalArgumentException( "attempt to use schema location with other than phyloXML parser" );
78         }
79         final PhyloXmlParser xml_parser = ( PhyloXmlParser ) parser;
80         if ( !ForesterUtil.isEmpty( schema_location ) ) {
81             xml_parser.setValidateAgainstSchema( schema_location );
82         }
83         xml_parser.setSource( source );
84         return xml_parser.parse();
85     }
86
87     public static PhylogenyFactory getInstance() {
88         return _instance;
89     }
90 }