2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26 package org.forester.io.parsers.phyloxml.data;
28 import java.util.SortedSet;
29 import java.util.TreeSet;
31 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
32 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
33 import org.forester.io.parsers.phyloxml.XmlElement;
34 import org.forester.phylogeny.data.BinaryCharacters;
35 import org.forester.phylogeny.data.PhylogenyData;
37 public class BinaryCharactersParser implements PhylogenyDataPhyloXmlParser {
39 private static final BinaryCharactersParser _instance;
42 _instance = new BinaryCharactersParser();
44 catch ( final Throwable e ) {
45 throw new RuntimeException( e.getMessage() );
49 private BinaryCharactersParser() {
53 public PhylogenyData parse( final XmlElement element ) throws PhyloXmlDataFormatException {
54 final SortedSet<String> present = new TreeSet<String>();
55 final SortedSet<String> gained = new TreeSet<String>();
56 final SortedSet<String> lost = new TreeSet<String>();
58 int present_count = BinaryCharacters.COUNT_DEFAULT;
59 int gained_count = BinaryCharacters.COUNT_DEFAULT;
60 int lost_count = BinaryCharacters.COUNT_DEFAULT;
61 if ( element.isHasAttribute( PhyloXmlMapping.BINARY_CHARACTERS_TYPE_ATTR ) ) {
62 type = element.getAttribute( PhyloXmlMapping.BINARY_CHARACTERS_TYPE_ATTR );
65 if ( element.isHasAttribute( PhyloXmlMapping.BINARY_CHARACTERS_PRESENT_COUNT_ATTR ) ) {
66 present_count = Integer.parseInt( element
67 .getAttribute( PhyloXmlMapping.BINARY_CHARACTERS_PRESENT_COUNT_ATTR ) );
69 if ( element.isHasAttribute( PhyloXmlMapping.BINARY_CHARACTERS_GAINED_COUNT_ATTR ) ) {
70 gained_count = Integer.parseInt( element
71 .getAttribute( PhyloXmlMapping.BINARY_CHARACTERS_GAINED_COUNT_ATTR ) );
73 if ( element.isHasAttribute( PhyloXmlMapping.BINARY_CHARACTERS_LOST_COUNT_ATTR ) ) {
75 .parseInt( element.getAttribute( PhyloXmlMapping.BINARY_CHARACTERS_LOST_COUNT_ATTR ) );
78 catch ( final NumberFormatException e ) {
79 throw new PhyloXmlDataFormatException( "failed to parse integer from element " + element.getQualifiedName() );
81 for( int i = 0; i < element.getNumberOfChildElements(); ++i ) {
82 final XmlElement child_element = element.getChildElement( i );
83 if ( child_element.getQualifiedName().equals( PhyloXmlMapping.BINARY_CHARACTERS_PRESENT ) ) {
84 parseCharacters( present, child_element );
86 else if ( child_element.getQualifiedName().equals( PhyloXmlMapping.BINARY_CHARACTERS_GAINED ) ) {
87 parseCharacters( gained, child_element );
89 else if ( child_element.getQualifiedName().equals( PhyloXmlMapping.BINARY_CHARACTERS_LOST ) ) {
90 parseCharacters( lost, child_element );
93 BinaryCharacters bc = null;
94 if ( present_count != BinaryCharacters.COUNT_DEFAULT ) {
95 bc = new BinaryCharacters( present, gained, lost, type, present_count, gained_count, lost_count );
98 bc = new BinaryCharacters( present, gained, lost, type );
103 private void parseCharacters( final SortedSet<String> present, final XmlElement child_element ) {
104 for( int j = 0; j < child_element.getNumberOfChildElements(); ++j ) {
105 final XmlElement child_child_element = child_element.getChildElement( j );
106 if ( child_child_element.getQualifiedName().equals( PhyloXmlMapping.BINARY_CHARACTER ) ) {
107 present.add( child_child_element.getValueAsString() );
112 public static PhylogenyDataPhyloXmlParser getInstance() {