Debian Codegraph @

src/dr/evoxml/CompositeDataTypeParser.java

raw symbols SCIP
1 /*
2 * CompositeDataTypeParser.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.datatype.DataType;
29 import dr.evolution.datatype.GeneralDataType;
30 import dr.util.Identifiable;
31 import dr.xml.*;
32
33 import java.util.ArrayList;
34 import java.util.List;
35
36 /**
37 * @author Andrew Rambaut
38 * @author Marc Suchard
39 *
40 * @version $Id: GeneralDataTypeParser.java,v 1.2 2005/05/24 20:25:59 rambaut Exp $
41 */
42 public class CompositeDataTypeParser extends AbstractXMLObjectParser {
43
44 /**
45 * Name of data type. For XML and human reading of data type.
46 */
47 public static final String COMPOSITE_DATA_TYPE = "compositeDataType";
48
49 public static final String COMPOSITE_STATE_SEPARATOR = "-";
50
51
52 public String getParserName() { return COMPOSITE_DATA_TYPE; }
53
54 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
55
56 List<String> states = new ArrayList<String>();
57
58 List<DataType> dataTypes = new ArrayList<DataType>();
59
60 for (int i =0; i < xo.getChildCount(); i++) {
61 if (xo.getChild(i) instanceof DataType) {
62 dataTypes.add((DataType)xo.getChild(i));
63 } else {
64 throw new XMLParseException("illegal element in " + getParserName() + " element");
65 }
66 }
67
68 if (dataTypes.size() != 2) {
69 throw new XMLParseException("CompositeDataType requires exactly 2 component DataTypes");
70 }
71
72 DataType dt1 = dataTypes.get(0);
73 DataType dt2 = dataTypes.get(1);
74
75 List<String> jointStates = new ArrayList<String>();
76
77 for (int state1 = 0; state1 < dt1.getStateCount(); state1++) {
78 String code1 = dt1.getCode(state1);
79 for (int state2 = 0; state2 < dt2.getStateCount(); state2++) {
80 String code2 = dt2.getCode(state2);
81 jointStates.add(code1 + COMPOSITE_STATE_SEPARATOR + code2);
82 }
83 }
84
85 if (states.size() == 0) {
86 throw new XMLParseException("No state elements defined in " + getParserName() + " element");
87 } else if (states.size() < 2 ) {
88 throw new XMLParseException("Fewer than two state elements defined in " + getParserName() + " element");
89 }
90
91 GeneralDataType dataType = new GeneralDataType(states);
92
93 return dataType;
94 }
95
96 //************************************************************************
97 // AbstractXMLObjectParser implementation
98 //************************************************************************
99
100 public String getParserDescription() {
101 return "Defines a composite DataType consisting of multiple data types";
102 }
103
104 public Class getReturnType() { return DataType.class; }
105
106 public XMLSyntaxRule[] getSyntaxRules() { return rules; }
107
108 private XMLSyntaxRule[] rules = new XMLSyntaxRule[] {
109 new ElementRule(DataType.class, 2, 2),
110 };
111 }