| 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.evolution.util.Date; |
| 29 |
import dr.evolution.util.Units; |
| 30 |
import dr.evoxml.util.DateUnitsType; |
| 31 |
import dr.evoxml.util.XMLUnits; |
| 32 |
import dr.xml.*; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class DateParser extends AbstractXMLObjectParser { |
| 40 |
|
| 41 |
public static final String VALUE = "value"; |
| 42 |
public static final String UNITS = "units"; |
| 43 |
public static final String ORIGIN = "origin"; |
| 44 |
public static final String DIRECTION = "direction"; |
| 45 |
|
| 46 |
public static final String FORWARDS = DateUnitsType.FORWARDS.getAttribute(); |
| 47 |
public static final String BACKWARDS = DateUnitsType.BACKWARDS.getAttribute(); |
| 48 |
|
| 49 |
public static final String YEARS = DateUnitsType.YEARS.getAttribute(); |
| 50 |
public static final String MONTHS = DateUnitsType.MONTHS.getAttribute(); |
| 51 |
public static final String DAYS = DateUnitsType.DAYS.getAttribute(); |
| 52 |
|
| 53 |
public static final String UNCERTAINTY = "uncertainty"; |
| 54 |
public static final String PRECISION = "precision"; |
| 55 |
|
| 56 |
public String getParserName() { |
| 57 |
return Date.DATE; |
| 58 |
} |
| 59 |
|
| 60 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 61 |
|
| 62 |
java.text.DateFormat dateFormat = java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT, java.util.Locale.UK); |
| 63 |
dateFormat.setLenient(true); |
| 64 |
|
| 65 |
if (xo.getChildCount() > 0) { |
| 66 |
throw new XMLParseException("No child elements allowed in date element."); |
| 67 |
} |
| 68 |
|
| 69 |
double value = 0.0; |
| 70 |
java.util.Date dateValue = null; |
| 71 |
|
| 72 |
if (xo.hasAttribute(VALUE)) { |
| 73 |
try { |
| 74 |
value = xo.getDoubleAttribute(VALUE); |
| 75 |
} catch (XMLParseException e) { |
| 76 |
String dateString = xo.getStringAttribute(VALUE); |
| 77 |
|
| 78 |
try { |
| 79 |
|
| 80 |
dateValue = dateFormat.parse(dateString); |
| 81 |
|
| 82 |
} catch (Exception ex) { |
| 83 |
throw new XMLParseException("value=" + dateString + " not recognised as a date, use DD/MM/YYYY"); |
| 84 |
} |
| 85 |
} |
| 86 |
} else { |
| 87 |
throw new XMLParseException("Value attribute missing from date element."); |
| 88 |
} |
| 89 |
|
| 90 |
boolean backwards = false; |
| 91 |
|
| 92 |
if (xo.hasAttribute(DIRECTION)) { |
| 93 |
String direction = (String) xo.getAttribute(DIRECTION); |
| 94 |
if (direction.equals(BACKWARDS)) { |
| 95 |
backwards = true; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
Units.Type units = XMLUnits.Utils.getUnitsAttr(xo); |
| 100 |
|
| 101 |
Date date; |
| 102 |
|
| 103 |
if (xo.hasAttribute(ORIGIN)) { |
| 104 |
|
| 105 |
String originString = (String) xo.getAttribute(ORIGIN); |
| 106 |
java.util.Date origin; |
| 107 |
|
| 108 |
try { |
| 109 |
origin = dateFormat.parse(originString); |
| 110 |
} catch (Exception e) { |
| 111 |
throw new XMLParseException("origin=" + originString + " not recognised as a date, use DD/MM/YYYY"); |
| 112 |
} |
| 113 |
|
| 114 |
if (dateValue != null) { |
| 115 |
date = new Date(dateValue, units, origin); |
| 116 |
} else { |
| 117 |
date = new Date(value, units, backwards, origin); |
| 118 |
} |
| 119 |
|
| 120 |
} else { |
| 121 |
|
| 122 |
|
| 123 |
if (dateValue != null) { |
| 124 |
date = new Date(dateValue, units); |
| 125 |
} else { |
| 126 |
date = new Date(value, units, backwards); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
if (xo.hasAttribute(UNCERTAINTY)) { |
| 131 |
double uncertainty = xo.getDoubleAttribute(UNCERTAINTY); |
| 132 |
date.setUncertainty(uncertainty); |
| 133 |
} else if (xo.hasAttribute(PRECISION)) { |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
double uncertainty = xo.getDoubleAttribute(PRECISION); |
| 138 |
date.setUncertainty(uncertainty); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
return date; |
| 143 |
} |
| 144 |
|
| 145 |
public String getParserDescription() { |
| 146 |
return "Specifies a date on a given timescale"; |
| 147 |
} |
| 148 |
|
| 149 |
public String getExample() { |
| 150 |
return |
| 151 |
"<!-- a date representing 10 years in the past -->\n" + |
| 152 |
"<date value=\"10.0\" units=\"years\" direction=\"backwards\"/>\n" + |
| 153 |
"\n" + |
| 154 |
"<!-- a date representing 300 days after Jan 1st 1989 -->\n" + |
| 155 |
"<date value=\"300.0\" origin=\"01/01/89\" units=\"days\" direction=\"forwards\"/>\n"; |
| 156 |
} |
| 157 |
|
| 158 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 159 |
return rules; |
| 160 |
} |
| 161 |
|
| 162 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 163 |
new StringAttributeRule(VALUE, |
| 164 |
"The value of this date"), |
| 165 |
new StringAttributeRule(ORIGIN, |
| 166 |
"The origin of this time scale, which must be a valid calendar date", "01/01/01", true), |
| 167 |
new StringAttributeRule(UNITS, "The units of the timescale", new String[]{YEARS, MONTHS, DAYS}, true), |
| 168 |
new StringAttributeRule(DIRECTION, "The direction of the timescale", new String[]{FORWARDS, BACKWARDS}, true), |
| 169 |
new XORRule( |
| 170 |
AttributeRule.newDoubleRule(UNCERTAINTY, false, "The uncertainty to which the date is specified"), |
| 171 |
AttributeRule.newDoubleRule(PRECISION, false, "The uncertainty to which the date is specified"), true |
| 172 |
) |
| 173 |
}; |
| 174 |
|
| 175 |
public Class getReturnType() { |
| 176 |
return dr.evolution.util.Date.class; |
| 177 |
} |
| 178 |
} |