in progress
[jalview.git] / forester / java / src / org / forester / io / parsers / phyloxml / PhyloXmlHandler.java
index 6c013f6..b724a79 100644 (file)
@@ -5,7 +5,7 @@
 // Copyright (C) 2008-2009 Christian M. Zmasek
 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
 // All rights reserved
-// 
+//
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
@@ -15,7 +15,7 @@
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 // Lesser General Public License for more details.
-// 
+//
 // You should have received a copy of the GNU Lesser General Public
 // License along with this library; if not, write to the Free Software
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
@@ -55,13 +55,14 @@ import org.forester.phylogeny.data.Date;
 import org.forester.phylogeny.data.Distribution;
 import org.forester.phylogeny.data.Event;
 import org.forester.phylogeny.data.Identifier;
+import org.forester.phylogeny.data.PhylogenyDataUtil;
 import org.forester.phylogeny.data.PropertiesMap;
 import org.forester.phylogeny.data.Property;
 import org.forester.phylogeny.data.Reference;
 import org.forester.phylogeny.data.Sequence;
 import org.forester.phylogeny.data.SequenceRelation;
-import org.forester.phylogeny.data.Taxonomy;
 import org.forester.phylogeny.data.SequenceRelation.SEQUENCE_RELATION_TYPE;
+import org.forester.phylogeny.data.Taxonomy;
 import org.forester.util.FailedConditionCheckException;
 import org.forester.util.ForesterConstants;
 import org.forester.util.ForesterUtil;
@@ -119,6 +120,9 @@ public final class PhyloXmlHandler extends DefaultHandler {
                 catch ( final PhylogenyParserException ex ) {
                     throw new SAXException( ex.getMessage() );
                 }
+                catch ( final PhyloXmlDataFormatException e ) {
+                    throw new SAXException( e.getMessage() );
+                }
             }
             else if ( local_name.equals( PhyloXmlMapping.SEQUENCE_RELATION ) ) {
                 try {
@@ -172,7 +176,7 @@ public final class PhyloXmlHandler extends DefaultHandler {
                         }
                     }
                 }
-                catch ( final PhylogenyParserException ex ) {
+                catch ( final PhyloXmlDataFormatException ex ) {
                     throw new SAXException( ex.getMessage() );
                 }
             }
@@ -180,8 +184,11 @@ public final class PhyloXmlHandler extends DefaultHandler {
                 try {
                     PhyloXmlHandler.mapElementToPhylogeny( getCurrentXmlElement(), getCurrentPhylogeny() );
                 }
-                catch ( final PhylogenyParserException ex ) {
-                    throw new SAXException( ex.getMessage() );
+                catch ( final PhylogenyParserException e ) {
+                    throw new SAXException( e.getMessage() );
+                }
+                catch ( final PhyloXmlDataFormatException e ) {
+                    throw new SAXException( e.getMessage() );
                 }
                 finishPhylogeny();
                 reset();
@@ -244,7 +251,7 @@ public final class PhyloXmlHandler extends DefaultHandler {
     }
 
     private void mapElementToPhylogenyNode( final XmlElement xml_element, final PhylogenyNode node )
-            throws PhylogenyParserException {
+            throws PhylogenyParserException, PhyloXmlDataFormatException {
         if ( xml_element.isHasAttribute( PhyloXmlMapping.BRANCH_LENGTH ) ) {
             double d = 0;
             try {
@@ -256,11 +263,17 @@ public final class PhyloXmlHandler extends DefaultHandler {
             }
             node.setDistanceToParent( d );
         }
+        if ( xml_element.isHasAttribute( PhyloXmlMapping.NODE_COLLAPSE ) ) {
+            final String collapse_str = xml_element.getAttribute( PhyloXmlMapping.NODE_COLLAPSE );
+            if ( !ForesterUtil.isEmpty( collapse_str ) && collapse_str.trim().equalsIgnoreCase( "true" ) ) {
+                node.setCollapse( true );
+            }
+        }
         for( int i = 0; i < xml_element.getNumberOfChildElements(); ++i ) {
             final XmlElement element = xml_element.getChildElement( i );
             final String qualified_name = element.getQualifiedName();
             if ( qualified_name.equals( PhyloXmlMapping.BRANCH_LENGTH ) ) {
-                if ( node.getDistanceToParent() != PhylogenyNode.DISTANCE_DEFAULT ) {
+                if ( node.getDistanceToParent() != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) {
                     throw new PhylogenyParserException( "ill advised attempt to set distance twice for the same clade (probably via element and via attribute)" );
                 }
                 node.setDistanceToParent( element.getValueAsDouble() );
@@ -312,8 +325,8 @@ public final class PhyloXmlHandler extends DefaultHandler {
                 if ( !node.getNodeData().isHasProperties() ) {
                     node.getNodeData().setProperties( new PropertiesMap() );
                 }
-                node.getNodeData().getProperties().addProperty( ( Property ) PropertyParser.getInstance()
-                        .parse( element ) );
+                node.getNodeData().getProperties()
+                        .addProperty( ( Property ) PropertyParser.getInstance().parse( element ) );
             }
         }
     }
@@ -385,8 +398,8 @@ public final class PhyloXmlHandler extends DefaultHandler {
                             .getAttribute( PhyloXmlMapping.PHYLOGENY_IS_REROOTABLE_ATTR ) ) );
                 }
                 if ( element.isHasAttribute( PhyloXmlMapping.PHYLOGENY_BRANCHLENGTH_UNIT_ATTR ) ) {
-                    getCurrentPhylogeny().setDistanceUnit( element
-                            .getAttribute( PhyloXmlMapping.PHYLOGENY_BRANCHLENGTH_UNIT_ATTR ) );
+                    getCurrentPhylogeny()
+                            .setDistanceUnit( element.getAttribute( PhyloXmlMapping.PHYLOGENY_BRANCHLENGTH_UNIT_ATTR ) );
                 }
                 if ( element.isHasAttribute( PhyloXmlMapping.PHYLOGENY_IS_ROOTED_ATTR ) ) {
                     getCurrentPhylogeny().setRooted( Boolean.parseBoolean( element
@@ -433,7 +446,7 @@ public final class PhyloXmlHandler extends DefaultHandler {
     }
 
     private static void mapElementToPhylogeny( final XmlElement xml_element, final Phylogeny phylogeny )
-            throws PhylogenyParserException {
+            throws PhylogenyParserException, PhyloXmlDataFormatException {
         for( int i = 0; i < xml_element.getNumberOfChildElements(); ++i ) {
             final XmlElement element = xml_element.getChildElement( i );
             final String qualified_name = element.getQualifiedName();