inprogress
[jalview.git] / forester / java / src / org / forester / io / parsers / phyloxml / data / PointParser.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: https://sites.google.com/site/cmzmasek/home/software/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.PhylogenyData;
34 import org.forester.phylogeny.data.Point;
35 import org.forester.util.ForesterUtil;
36
37 public class PointParser implements PhylogenyDataPhyloXmlParser {
38
39     private static final PhylogenyDataPhyloXmlParser _instance;
40     static {
41         try {
42             _instance = new PointParser();
43         }
44         catch ( final Throwable e ) {
45             throw new RuntimeException( e.getMessage() );
46         }
47     }
48
49     private PointParser() {
50     }
51
52     @Override
53     public PhylogenyData parse( final XmlElement element ) throws PhyloXmlDataFormatException {
54         String alt_unit = "";
55         String geo_datum = "";
56         if ( element.isHasAttribute( PhyloXmlMapping.POINT_ALTITUDE_UNIT_ATTR ) ) {
57             alt_unit = element.getAttribute( PhyloXmlMapping.POINT_ALTITUDE_UNIT_ATTR );
58         }
59         if ( element.isHasAttribute( PhyloXmlMapping.POINT_GEODETIC_DATUM ) ) {
60             geo_datum = element.getAttribute( PhyloXmlMapping.POINT_GEODETIC_DATUM );
61         }
62         String lat_str = null;
63         String lon_str = null;
64         String alt_str = null;
65         for( int j = 0; j < element.getNumberOfChildElements(); ++j ) {
66             final XmlElement e = element.getChildElement( j );
67             if ( e.getQualifiedName().equals( PhyloXmlMapping.POINT_LATITUDE ) ) {
68                 lat_str = e.getValueAsString();
69             }
70             else if ( e.getQualifiedName().equals( PhyloXmlMapping.POINT_LONGITUDE ) ) {
71                 lon_str = e.getValueAsString();
72             }
73             else if ( e.getQualifiedName().equals( PhyloXmlMapping.POINT_ALTITUDE ) ) {
74                 alt_str = e.getValueAsString();
75             }
76         }
77         BigDecimal lat = null;
78         BigDecimal lon = null;
79         BigDecimal alt = null;
80         if ( !ForesterUtil.isEmpty( lat_str ) ) {
81             lat = new BigDecimal( lat_str );
82         }
83         if ( !ForesterUtil.isEmpty( lon_str ) ) {
84             lon = new BigDecimal( lon_str );
85         }
86         if ( !ForesterUtil.isEmpty( alt_str ) ) {
87             alt = new BigDecimal( alt_str );
88         }
89         return new Point( geo_datum, lat, lon, alt, alt_unit );
90     }
91
92     public static PhylogenyDataPhyloXmlParser getInstance() {
93         return _instance;
94     }
95 }