| 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.Location; |
| 30 |
import dr.evolution.util.Taxon; |
| 31 |
import dr.util.Attribute; |
| 32 |
import dr.xml.*; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class TaxonParser extends AbstractXMLObjectParser { |
| 40 |
|
| 41 |
public final static String TAXON = "taxon"; |
| 42 |
|
| 43 |
public String getParserName() { |
| 44 |
return TAXON; |
| 45 |
} |
| 46 |
|
| 47 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 48 |
|
| 49 |
if (dr.xml.XMLParser.ID.contains("\'") && dr.xml.XMLParser.ID.contains("\"")) { |
| 50 |
|
| 51 |
|
| 52 |
throw new XMLParseException("Illegal taxon name, " + dr.xml.XMLParser.ID + ", - contains both single and double quotes"); |
| 53 |
} |
| 54 |
|
| 55 |
Taxon taxon = new Taxon(xo.getStringAttribute(dr.xml.XMLParser.ID)); |
| 56 |
|
| 57 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 58 |
Object child = xo.getChild(i); |
| 59 |
|
| 60 |
if (child instanceof Date) { |
| 61 |
taxon.setDate((Date) child); |
| 62 |
} else if (child instanceof Location) { |
| 63 |
taxon.setLocation((Location) child); |
| 64 |
} else if (child instanceof Attribute) { |
| 65 |
final Attribute attr = (Attribute) child; |
| 66 |
taxon.setAttribute(attr.getAttributeName(), attr.getAttributeValue()); |
| 67 |
} else if (child instanceof Attribute[]) { |
| 68 |
Attribute[] attrs = (Attribute[]) child; |
| 69 |
for (Attribute attr : attrs) { |
| 70 |
taxon.setAttribute(attr.getAttributeName(), attr.getAttributeValue()); |
| 71 |
} |
| 72 |
} else { |
| 73 |
throw new XMLParseException("Unrecognized element found in taxon element"); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
return taxon; |
| 78 |
} |
| 79 |
|
| 80 |
public String getParserDescription() { |
| 81 |
return ""; |
| 82 |
} |
| 83 |
|
| 84 |
public Class getReturnType() { |
| 85 |
return Taxon.class; |
| 86 |
} |
| 87 |
|
| 88 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 89 |
return rules; |
| 90 |
} |
| 91 |
|
| 92 |
private final XMLSyntaxRule[] rules = { |
| 93 |
new StringAttributeRule(dr.xml.XMLParser.ID, "A unique identifier for this taxon"), |
| 94 |
new ElementRule(Attribute.Default.class, true), |
| 95 |
new ElementRule(Date.class, true), |
| 96 |
new ElementRule(Location.class, true) |
| 97 |
}; |
| 98 |
|
| 99 |
} |