| 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.tree.SimpleNode; |
| 29 |
import dr.evolution.util.Date; |
| 30 |
import dr.evolution.util.Taxon; |
| 31 |
import dr.util.Attributable; |
| 32 |
import dr.util.Attribute; |
| 33 |
import dr.xml.*; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class SimpleNodeParser extends AbstractXMLObjectParser { |
| 42 |
|
| 43 |
public final static String NODE = "node"; |
| 44 |
public final static String HEIGHT = "height"; |
| 45 |
public final static String RATE = "rate"; |
| 46 |
|
| 47 |
public String getParserName() { return NODE; } |
| 48 |
|
| 49 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 50 |
|
| 51 |
SimpleNode node = new SimpleNode(); |
| 52 |
|
| 53 |
Taxon taxon = null; |
| 54 |
|
| 55 |
if (xo.hasAttribute(HEIGHT)) { |
| 56 |
node.setHeight(xo.getDoubleAttribute(HEIGHT)); |
| 57 |
} |
| 58 |
|
| 59 |
if (xo.hasAttribute(RATE)) { |
| 60 |
node.setRate(xo.getDoubleAttribute(RATE)); |
| 61 |
} |
| 62 |
|
| 63 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 64 |
Object child = xo.getChild(i); |
| 65 |
if (child instanceof dr.evolution.tree.SimpleNode) { |
| 66 |
node.addChild((dr.evolution.tree.SimpleNode)child); |
| 67 |
} else if (child instanceof Taxon) { |
| 68 |
taxon = (Taxon)child; |
| 69 |
} else if (child instanceof Date) { |
| 70 |
node.setAttribute("date", child); |
| 71 |
} else if (child instanceof Attribute) { |
| 72 |
Attribute attr = (Attribute)child; |
| 73 |
String name = attr.getAttributeName(); |
| 74 |
Object value = attr.getAttributeValue(); |
| 75 |
node.setAttribute(name, value); |
| 76 |
} else if (child instanceof Attribute[]) { |
| 77 |
Attribute[] attrs = (Attribute[])child; |
| 78 |
for (int j =0; j < attrs.length; j++) { |
| 79 |
String name = attrs[j].getAttributeName(); |
| 80 |
Object value = attrs[j].getAttributeValue(); |
| 81 |
node.setAttribute(name, value); |
| 82 |
} |
| 83 |
} else if (child instanceof XMLObject) { |
| 84 |
XMLObject xoc = (XMLObject)child; |
| 85 |
if (xoc.getName().equals(Attributable.ATTRIBUTE)) { |
| 86 |
node.setAttribute(xoc.getStringAttribute(Attributable.NAME), xoc.getAttribute(Attributable.VALUE)); |
| 87 |
} else { |
| 88 |
throw new XMLParseException("Unrecognized element" + xoc.getName() + " found in node element!"); |
| 89 |
} |
| 90 |
} else { |
| 91 |
throw new XMLParseException("Unrecognized element found in node element!"); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
if (taxon != null) { |
| 96 |
node.setTaxon(taxon); |
| 97 |
} |
| 98 |
|
| 99 |
return node; |
| 100 |
} |
| 101 |
|
| 102 |
public String getParserDescription() { |
| 103 |
return "This element represents a node in a tree."; |
| 104 |
} |
| 105 |
|
| 106 |
public Class getReturnType() { return SimpleNode.class; } |
| 107 |
|
| 108 |
public XMLSyntaxRule[] getSyntaxRules() { return rules; } |
| 109 |
|
| 110 |
private final XMLSyntaxRule[] rules = { |
| 111 |
AttributeRule.newDoubleRule(HEIGHT, true, "the age of the node"), |
| 112 |
AttributeRule.newDoubleRule(RATE, true, "the relative rate of evolution at this node - default is 1.0"), |
| 113 |
new XORRule( |
| 114 |
new ElementRule(Taxon.class, "The taxon of this leaf node"), |
| 115 |
new ElementRule(SimpleNode.class, "The children of this internal node", 2, 2)) |
| 116 |
|
| 117 |
}; |
| 118 |
} |