| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.evoxml; |
| 27 |
|
| 28 |
import dr.xml.*; |
| 29 |
import dr.evolution.util.Location; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 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 |
|
| 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 |
} |