f072c58ff2dde2fa912442bfdf301304d5aff7fd
[jalview.git] / forester / java / src / org / forester / io / writers / PhyloXmlNodeWriter.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2000-2009 Christian M. Zmasek
6 // Copyright (C) 2007-2009 Burnham Institute for Medical Research
7 // All rights reserved
8 //
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.
13 //
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.
18 //
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
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.io.writers;
27
28 import java.io.IOException;
29 import java.io.Writer;
30
31 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
32 import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
33 import org.forester.phylogeny.PhylogenyNode;
34 import org.forester.phylogeny.data.PhylogenyDataUtil;
35 import org.forester.util.ForesterUtil;
36
37 public class PhyloXmlNodeWriter {
38
39     public static void toPhyloXml( final Writer w, final PhylogenyNode node, final int level, final String indentation )
40             throws IOException {
41         String ind = "";
42         if ( ( indentation != null ) && ( indentation.length() > 0 ) ) {
43             ind = indentation + PhylogenyWriter.PHYLO_XML_INTENDATION_BASE;
44         }
45         if ( !ForesterUtil.isEmpty( node.getName() ) ) {
46             PhylogenyDataUtil.appendElement( w, PhyloXmlMapping.NODE_NAME, node.getName(), indentation );
47         }
48         if ( node.getDistanceToParent() != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) {
49             PhylogenyDataUtil.appendElement( w, PhyloXmlMapping.BRANCH_LENGTH, String.valueOf( ForesterUtil.round( node
50                     .getDistanceToParent(), PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) ), indentation );
51         }
52         if ( node.getBranchData() != null ) {
53             node.getBranchData().toPhyloXML( w, level, ind );
54         }
55         if ( node.getNodeData() != null ) {
56             node.getNodeData().toPhyloXML( w, level, ind );
57         }
58     }
59 }