clean up
[jalview.git] / forester / java / src / org / forester / io / parsers / phyloxml / data / EventParser.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
25
26 package org.forester.io.parsers.phyloxml.data;
27
28 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
29 import org.forester.io.parsers.phyloxml.XmlElement;
30 import org.forester.io.parsers.util.PhylogenyParserException;
31 import org.forester.phylogeny.data.Confidence;
32 import org.forester.phylogeny.data.Event;
33 import org.forester.phylogeny.data.PhylogenyData;
34 import org.forester.util.ForesterUtil;
35
36 public class EventParser implements PhylogenyDataPhyloXmlParser {
37
38     private static final PhylogenyDataPhyloXmlParser _instance;
39     static {
40         try {
41             _instance = new EventParser();
42         }
43         catch ( final Throwable e ) {
44             throw new RuntimeException( e.getMessage() );
45         }
46     }
47
48     private EventParser() {
49     }
50
51     @Override
52     public PhylogenyData parse( final XmlElement element ) throws PhylogenyParserException {
53         String type = "";
54         Confidence conf = null;
55         int duplications = Event.DEFAULT_VALUE;
56         int speciations = Event.DEFAULT_VALUE;
57         int losses = Event.DEFAULT_VALUE;
58         for( int i = 0; i < element.getNumberOfChildElements(); ++i ) {
59             final XmlElement child_element = element.getChildElement( i );
60             if ( child_element.getQualifiedName().equals( PhyloXmlMapping.EVENT_TYPE ) ) {
61                 type = child_element.getValueAsString();
62             }
63             else if ( child_element.getQualifiedName().equals( PhyloXmlMapping.CONFIDENCE ) ) {
64                 conf = ( ( Confidence ) ConfidenceParser.getInstance().parse( child_element ) );
65             }
66             else if ( child_element.getQualifiedName().equals( PhyloXmlMapping.EVENT_DUPLICATIONS ) ) {
67                 duplications = child_element.getValueAsInt();
68             }
69             else if ( child_element.getQualifiedName().equals( PhyloXmlMapping.EVENT_SPECIATIONS ) ) {
70                 speciations = child_element.getValueAsInt();
71             }
72             else if ( child_element.getQualifiedName().equals( PhyloXmlMapping.EVENT_LOSSES ) ) {
73                 losses = child_element.getValueAsInt();
74             }
75         }
76         Event event = null;
77         if ( ForesterUtil.isEmpty( type ) ) {
78             event = new Event( duplications, speciations, losses );
79         }
80         else {
81             try {
82                 event = new Event( duplications, speciations, losses, type );
83             }
84             catch ( final Exception e ) {
85                 throw new PhylogenyParserException( "problem with " + element.toString() + ": " + e.getMessage() );
86             }
87         }
88         if ( conf != null ) {
89             event.setConfidence( conf );
90         }
91         return event;
92     }
93
94     public static PhylogenyDataPhyloXmlParser getInstance() {
95         return _instance;
96     }
97 }