Debian Codegraph @

src/dr/evoxml/SimpleNodeParser.java

raw symbols SCIP
1 /*
2 * SimpleNodeParser.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.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 * @author Alexei Drummond
37 * @author Andrew Rambaut
38 *
39 * @version $Id: SimpleNodeParser.java,v 1.2 2005/05/24 20:25:59 rambaut Exp $
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 }