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.phylogeny.data;
28 import java.awt.Graphics;
29 import java.io.IOException;
30 import java.io.Writer;
31 import java.util.ArrayList;
33 import org.forester.io.writers.PhylogenyWriter;
34 import org.forester.util.ForesterUtil;
36 public final class PhylogenyDataUtil {
38 /** Value of -99.0 is used as default value. */
39 public final static double BRANCH_LENGTH_DEFAULT = -1024.0;
41 public static void appendClose( final Writer w, final String element_name ) throws IOException {
43 w.write( element_name );
47 public static void appendElement( final Writer w, final String element_name, final String value )
49 appendOpen( w, element_name );
50 w.write( replaceIllegalXmlCharacters( value ) );
51 appendClose( w, element_name );
54 public static void appendElement( final Writer w,
55 final String element_name,
57 final String indentation ) throws IOException {
58 w.write( ForesterUtil.LINE_SEPARATOR );
59 w.write( indentation );
60 w.write( PhylogenyWriter.PHYLO_XML_INTENDATION_BASE );
61 // Something like this replacement needs to be done in a more systematic manner.
62 appendElement( w, element_name, value );
65 public static void appendElement( final Writer w,
66 final String element_name,
68 final String attribute_name,
69 final String attribute_value ) throws IOException {
70 appendOpen( w, element_name, attribute_name, attribute_value );
71 w.write( replaceIllegalXmlCharacters( value ) );
72 appendClose( w, element_name );
75 public static void appendElement( final Writer w,
76 final String element_name,
78 final String attribute_name,
79 final String attribute_value,
80 final String indentation ) throws IOException {
81 w.write( ForesterUtil.LINE_SEPARATOR );
82 w.write( indentation );
83 w.write( PhylogenyWriter.PHYLO_XML_INTENDATION_BASE );
84 appendOpen( w, element_name, attribute_name, attribute_value );
85 w.write( replaceIllegalXmlCharacters( value ) );
86 appendClose( w, element_name );
89 public static void appendElement( final Writer w,
90 final String element_name,
92 final String attribute1_name,
93 final String attribute1_value,
94 final String attribute2_name,
95 final String attribute2_value,
96 final String indentation ) throws IOException {
97 w.write( ForesterUtil.LINE_SEPARATOR );
98 w.write( indentation );
99 w.write( PhylogenyWriter.PHYLO_XML_INTENDATION_BASE );
100 appendOpen( w, element_name, attribute1_name, attribute1_value, attribute2_name, attribute2_value );
101 w.write( replaceIllegalXmlCharacters( value ) );
102 appendClose( w, element_name );
105 public static void appendElement( final Writer w,
106 final String element_name,
108 final String attribute1_name,
109 final String attribute1_value,
110 final String attribute2_name,
111 final String attribute2_value ) throws IOException {
112 appendOpen( w, element_name, attribute1_name, attribute1_value, attribute2_name, attribute2_value );
113 w.write( replaceIllegalXmlCharacters( value ) );
114 appendClose( w, element_name );
117 public static void appendElement( final Writer w,
118 final String element_name,
119 final String attribute1_name,
120 final String attribute1_value,
121 final String attribute2_name,
122 final String attribute2_value,
123 final String attribute3_name,
124 final String attribute3_value,
125 final String attribute4_name,
126 final String attribute4_value,
127 final String indentation ) throws IOException {
128 w.write( ForesterUtil.LINE_SEPARATOR );
129 w.write( indentation );
140 appendClose( w, element_name );
143 public static void appendElement( final Writer w,
144 final String element_name,
146 final String attribute1_name,
147 final String attribute1_value,
148 final String attribute2_name,
149 final String attribute2_value,
150 final String attribute3_name,
151 final String attribute3_value,
152 final String attribute4_name,
153 final String attribute4_value,
154 final String attribute5_name,
155 final String attribute5_value,
156 final String indentation ) throws IOException {
157 w.write( ForesterUtil.LINE_SEPARATOR );
158 w.write( indentation );
159 w.write( PhylogenyWriter.PHYLO_XML_INTENDATION_BASE );
172 w.write( replaceIllegalXmlCharacters( value ) );
173 appendClose( w, element_name );
176 public static void appendOpen( final Writer w, final String element_name ) throws IOException {
178 w.write( element_name );
182 public static void appendOpen( final Writer w,
183 final String element_name,
184 final String attribute_name,
185 final String attribute_value ) throws IOException {
187 w.write( element_name );
188 if ( !ForesterUtil.isEmpty( attribute_value ) ) {
190 w.write( attribute_name );
192 w.write( attribute_value );
198 public static void appendOpen( final Writer w,
199 final String element_name,
200 final String attribute1_name,
201 final String attribute1_value,
202 final String attribute2_name,
203 final String attribute2_value ) throws IOException {
205 w.write( element_name );
206 if ( !ForesterUtil.isEmpty( attribute1_value ) ) {
208 w.write( attribute1_name );
210 w.write( attribute1_value );
213 if ( !ForesterUtil.isEmpty( attribute2_value ) ) {
215 w.write( attribute2_name );
217 w.write( attribute2_value );
223 public static void appendOpen( final Writer w,
224 final String element_name,
225 final String attribute1_name,
226 final String attribute1_value,
227 final String attribute2_name,
228 final String attribute2_value,
229 final String attribute3_name,
230 final String attribute3_value ) throws IOException {
232 w.write( element_name );
233 if ( !ForesterUtil.isEmpty( attribute1_value ) ) {
235 w.write( attribute1_name );
237 w.write( attribute1_value );
240 if ( !ForesterUtil.isEmpty( attribute2_value ) ) {
242 w.write( attribute2_name );
244 w.write( attribute2_value );
247 if ( !ForesterUtil.isEmpty( attribute2_value ) ) {
249 w.write( attribute3_name );
251 w.write( attribute3_value );
257 public static void appendOpen( final Writer w,
258 final String element_name,
259 final String attribute1_name,
260 final String attribute1_value,
261 final String attribute2_name,
262 final String attribute2_value,
263 final String attribute3_name,
264 final String attribute3_value,
265 final String attribute4_name,
266 final String attribute4_value ) throws IOException {
268 w.write( element_name );
269 if ( !ForesterUtil.isEmpty( attribute1_value ) ) {
271 w.write( attribute1_name );
273 w.write( attribute1_value );
276 if ( !ForesterUtil.isEmpty( attribute2_value ) ) {
278 w.write( attribute2_name );
280 w.write( attribute2_value );
283 if ( !ForesterUtil.isEmpty( attribute3_value ) ) {
285 w.write( attribute3_name );
287 w.write( attribute3_value );
290 if ( !ForesterUtil.isEmpty( attribute4_value ) ) {
292 w.write( attribute4_name );
294 w.write( attribute4_value );
300 public static void appendOpen( final Writer w,
301 final String element_name,
302 final String attribute1_name,
303 final String attribute1_value,
304 final String attribute2_name,
305 final String attribute2_value,
306 final String attribute3_name,
307 final String attribute3_value,
308 final String attribute4_name,
309 final String attribute4_value,
310 final String attribute5_name,
311 final String attribute5_value ) throws IOException {
313 w.write( element_name );
314 if ( !ForesterUtil.isEmpty( attribute1_value ) ) {
316 w.write( attribute1_name );
318 w.write( attribute1_value );
321 if ( !ForesterUtil.isEmpty( attribute2_value ) ) {
323 w.write( attribute2_name );
325 w.write( attribute2_value );
328 if ( !ForesterUtil.isEmpty( attribute3_value ) ) {
330 w.write( attribute3_name );
332 w.write( attribute3_value );
335 if ( !ForesterUtil.isEmpty( attribute4_value ) ) {
337 w.write( attribute4_name );
339 w.write( attribute4_value );
342 if ( !ForesterUtil.isEmpty( attribute5_value ) ) {
344 w.write( attribute5_name );
346 w.write( attribute5_value );
353 * Creates a deep copy of ArrayList of PhylogenyData objects.
356 * an ArrayList of PhylogenyData objects
357 * @return a deep copy of ArrayList list
359 public static ArrayList<PhylogenyData> copy( final ArrayList<PhylogenyData> list ) {
360 final ArrayList<PhylogenyData> l = new ArrayList<PhylogenyData>( list.size() );
361 for( int i = 0; i < list.size(); ++i ) {
362 l.add( ( list.get( i ) ).copy() );
367 public static void drawLine( final double x1, final double y1, final double x2, final double y2, final Graphics g ) {
368 g.drawLine( org.forester.util.ForesterUtil.roundToInt( x1 ),
369 org.forester.util.ForesterUtil.roundToInt( y1 ),
370 org.forester.util.ForesterUtil.roundToInt( x2 ),
371 org.forester.util.ForesterUtil.roundToInt( y2 ) );
374 public static void drawString( final String str, final double x, final double y, final Graphics g ) {
376 org.forester.util.ForesterUtil.roundToInt( x ),
377 org.forester.util.ForesterUtil.roundToInt( y ) );
380 public static String replaceIllegalXmlCharacters( final String value ) {
381 String v = value.replaceAll( "&", "&" );
382 v = v.replaceAll( "<", "<" );
383 v = v.replaceAll( ">", ">" );
384 v = v.replaceAll( "'", "'" );
385 v = v.replaceAll( "\"", """ );