Debian Codegraph @

src/dr/evoxml/TaxonParser.java

raw symbols SCIP
1 /*
2 * TaxonParser.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.Location;
30 import dr.evolution.util.Taxon;
31 import dr.util.Attribute;
32 import dr.xml.*;
33
34 /**
35 * @author Alexei Drummond
36 * @author Andrew Rambaut
37 * @version $Id: TaxonParser.java,v 1.2 2005/05/24 20:25:59 rambaut Exp $
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 // unable to handle taxon names that contain both single and double quotes
51 // as it won't be possible to wrap it in either.
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 }