Debian Codegraph @

src/dr/evoxml/DateParser.java

raw symbols SCIP
1 /*
2 * DateParser.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.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 * @author Alexei Drummond
36 * @author Andrew Rambaut
37 * @version $Id: DateParser.java,v 1.2 2005/05/24 20:25:59 rambaut Exp $
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(); //"forwards";
47 public static final String BACKWARDS = DateUnitsType.BACKWARDS.getAttribute(); //"backwards";
48
49 public static final String YEARS = DateUnitsType.YEARS.getAttribute(); //"units";
50 public static final String MONTHS = DateUnitsType.MONTHS.getAttribute(); //"units";
51 public static final String DAYS = DateUnitsType.DAYS.getAttribute(); //"days";
52
53 public static final String UNCERTAINTY = "uncertainty";
54 public static final String PRECISION = "precision"; // inappropriate older attribute
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 // No origin specified so use default (1st Jan 1970)
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 // Precision was the old term for uncertainty which was confusing
135 // because it was the reciprocal. Parse this term synonymously for
136 // backwards compatibility.
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 }