Debian Codegraph @

src/dr/evoxml/LocationParser.java

raw symbols SCIP
1 /*
2 * LocationParser.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST 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
18 * GNU 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 BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evoxml;
27
28 import dr.xml.*;
29 import dr.evolution.util.Location;
30
31 /**
32 * @author Andrew Rambaut
33 *
34 * @version $Id: DateParser.java,v 1.2 2005/05/24 20:25:59 rambaut Exp $
35 */
36 public class LocationParser extends AbstractXMLObjectParser {
37
38 public static final String DESCRIPTION = "description";
39 public static final String LONGITUDE = "longitude";
40 public static final String LATITUDE = "latitude";
41
42 public String getParserName() { return Location.LOCATION; }
43
44 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
45
46 if (xo.getChildCount() > 0) {
47 throw new XMLParseException("No child elements allowed in location element.");
48 }
49
50 String description = xo.getAttribute(DESCRIPTION, "");
51
52 double longitude = parseLongLat(xo.getAttribute(LONGITUDE, ""));
53 double latitude = parseLongLat(xo.getAttribute(LATITUDE, ""));
54
55 return Location.newLocation(xo.getId(), description, longitude, latitude);
56 }
57
58 private double parseLongLat(final String value) throws XMLParseException {
59 double d = 0.0;
60
61 if (value != null && value.length() > 0) {
62 try {
63 d = Double.parseDouble(value);
64 } catch (NumberFormatException nfe) {
65 // @todo - parse degrees minutes and seconds
66 }
67 }
68
69 return d;
70 }
71
72 public String getParserDescription() {
73 return "Specifies a location with an optional longitude and latitude";
74 }
75
76 public XMLSyntaxRule[] getSyntaxRules() { return rules; }
77
78 private XMLSyntaxRule[] rules = new XMLSyntaxRule[] {
79 AttributeRule.newStringRule(DESCRIPTION, true,
80 "A description of this location"),
81 AttributeRule.newStringRule(LONGITUDE, true,
82 "The longitude in degrees, minutes, seconds or decimal degrees"),
83 AttributeRule.newStringRule(LATITUDE, true,
84 "The latitude in degrees, minutes, seconds or decimal degrees"),
85 };
86
87 public Class getReturnType() { return Location.class; }
88 }