780b84d9f1600c8114aa789895edd64d84932ec8
[jalview.git] / forester / java / src / org / forester / io / parsers / phyloxml / data / DateParser.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 java.math.BigDecimal;
29
30 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
31 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
32 import org.forester.io.parsers.phyloxml.XmlElement;
33 import org.forester.phylogeny.data.Date;
34 import org.forester.phylogeny.data.PhylogenyData;
35 import org.forester.util.ForesterUtil;
36
37 public class DateParser implements PhylogenyDataPhyloXmlParser {
38
39     private static final PhylogenyDataPhyloXmlParser _instance;
40     static {
41         try {
42             _instance = new DateParser();
43         }
44         catch ( final Throwable e ) {
45             throw new RuntimeException( e.getMessage() );
46         }
47     }
48
49     private DateParser() {
50     }
51
52     @Override
53     public PhylogenyData parse( final XmlElement element ) throws PhyloXmlDataFormatException {
54         String unit = "";
55         if ( element.isHasAttribute( PhyloXmlMapping.CLADE_DATE_UNIT ) ) {
56             unit = element.getAttribute( PhyloXmlMapping.CLADE_DATE_UNIT );
57         }
58         String val = null;
59         String min = null;
60         String max = null;
61         String desc = "";
62         for( int j = 0; j < element.getNumberOfChildElements(); ++j ) {
63             final XmlElement e = element.getChildElement( j );
64             if ( e.getQualifiedName().equals( PhyloXmlMapping.CLADE_DATE_VALUE ) ) {
65                 val = e.getValueAsString();
66             }
67             else if ( e.getQualifiedName().equals( PhyloXmlMapping.CLADE_DATE_MIN ) ) {
68                 min = e.getValueAsString();
69             }
70             else if ( e.getQualifiedName().equals( PhyloXmlMapping.CLADE_DATE_MAX ) ) {
71                 max = e.getValueAsString();
72             }
73             else if ( e.getQualifiedName().equals( PhyloXmlMapping.CLADE_DATE_DESC ) ) {
74                 desc = e.getValueAsString();
75             }
76         }
77         BigDecimal val_bd = null;
78         BigDecimal min_bd = null;
79         BigDecimal max_bd = null;
80         if ( !ForesterUtil.isEmpty( val ) ) {
81             val_bd = new BigDecimal( val );
82         }
83         if ( !ForesterUtil.isEmpty( min ) ) {
84             min_bd = new BigDecimal( min );
85         }
86         if ( !ForesterUtil.isEmpty( max ) ) {
87             max_bd = new BigDecimal( max );
88         }
89         return new Date( desc, val_bd, min_bd, max_bd, unit );
90     }
91
92     public static PhylogenyDataPhyloXmlParser getInstance() {
93         return _instance;
94     }
95 }