initial commit
[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     public synchronized Phylogeny[] create( final Object source, final Object parser, final List<Object> parameters )
59             throws IOException {
60         if ( !( parser instanceof PhylogenyParser ) ) {
61             throw new IllegalArgumentException( "attempt to use object of type other than PhylogenyParser as creator for ParserBasedPhylogenyFactory" );
62         }
63         final PhylogenyParser my_parser = ( PhylogenyParser ) parser;
64         my_parser.setSource( source );
65         return my_parser.parse();
66     }
67
68     public synchronized Phylogeny[] create( final Object source,
69                                             final Object parser,
70                                             final String schema_location,
71                                             final List<Object> parameters ) throws IOException {
72         if ( !( parser instanceof PhylogenyParser ) ) {
73             throw new IllegalArgumentException( "attempt to use object of type other than PhylogenyParser as creator for ParserBasedPhylogenyFactory." );
74         }
75         if ( !( parser instanceof PhyloXmlParser ) ) {
76             throw new IllegalArgumentException( "attempt to use schema location with other than phyloXML parser" );
77         }
78         final PhyloXmlParser xml_parser = ( PhyloXmlParser ) parser;
79         if ( !ForesterUtil.isEmpty( schema_location ) ) {
80             xml_parser.setValidateAgainstSchema( schema_location );
81         }
82         xml_parser.setSource( source );
83         return xml_parser.parse();
84     }
85
86     public static PhylogenyFactory getInstance() {
87         return _instance;
88     }
89 }