76d60b7951001eea9d08909274929f67d00439bd
[jalview.git] / forester / java / src / org / forester / phylogeny / data / PhylogenyDataUtil.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-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.phylogeny.data;
27
28 import java.awt.Graphics;
29 import java.io.IOException;
30 import java.io.Writer;
31 import java.util.ArrayList;
32
33 import org.forester.io.writers.PhylogenyWriter;
34 import org.forester.util.ForesterUtil;
35
36 public final class PhylogenyDataUtil {
37
38     /** Value of -99.0 is used as default value. */
39     public final static double BRANCH_LENGTH_DEFAULT = -1024.0;
40
41     public static void appendClose( final Writer w, final String element_name ) throws IOException {
42         w.write( "</" );
43         w.write( element_name );
44         w.write( ">" );
45     }
46
47     public static void appendElement( final Writer w, final String element_name, final String value )
48             throws IOException {
49         appendOpen( w, element_name );
50         w.write( replaceIllegalXmlCharacters( value ) );
51         appendClose( w, element_name );
52     }
53
54     public static void appendElement( final Writer w,
55                                       final String element_name,
56                                       final String value,
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 );
63     }
64
65     public static void appendElement( final Writer w,
66                                       final String element_name,
67                                       final String value,
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 );
73     }
74
75     public static void appendElement( final Writer w,
76                                       final String element_name,
77                                       final String value,
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 );
87     }
88
89     public static void appendElement( final Writer w,
90                                       final String element_name,
91                                       final String value,
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 );
103     }
104
105     public static void appendElement( final Writer w,
106                                       final String element_name,
107                                       final String value,
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 );
115     }
116
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 );
130         appendOpen( w,
131                     element_name,
132                     attribute1_name,
133                     attribute1_value,
134                     attribute2_name,
135                     attribute2_value,
136                     attribute3_name,
137                     attribute3_value,
138                     attribute4_name,
139                     attribute4_value );
140         appendClose( w, element_name );
141     }
142
143     public static void appendElement( final Writer w,
144                                       final String element_name,
145                                       final String value,
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 );
160         appendOpen( w,
161                     element_name,
162                     attribute1_name,
163                     attribute1_value,
164                     attribute2_name,
165                     attribute2_value,
166                     attribute3_name,
167                     attribute3_value,
168                     attribute4_name,
169                     attribute4_value,
170                     attribute5_name,
171                     attribute5_value );
172         w.write( replaceIllegalXmlCharacters( value ) );
173         appendClose( w, element_name );
174     }
175
176     public static void appendOpen( final Writer w, final String element_name ) throws IOException {
177         w.write( "<" );
178         w.write( element_name );
179         w.write( ">" );
180     }
181
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 {
186         w.write( "<" );
187         w.write( element_name );
188         if ( !ForesterUtil.isEmpty( attribute_value ) ) {
189             w.write( " " );
190             w.write( attribute_name );
191             w.write( "=\"" );
192             w.write( attribute_value );
193             w.write( "\"" );
194         }
195         w.write( ">" );
196     }
197
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 {
204         w.write( "<" );
205         w.write( element_name );
206         if ( !ForesterUtil.isEmpty( attribute1_value ) ) {
207             w.write( " " );
208             w.write( attribute1_name );
209             w.write( "=\"" );
210             w.write( attribute1_value );
211             w.write( "\"" );
212         }
213         if ( !ForesterUtil.isEmpty( attribute2_value ) ) {
214             w.write( " " );
215             w.write( attribute2_name );
216             w.write( "=\"" );
217             w.write( attribute2_value );
218             w.write( "\"" );
219         }
220         w.write( ">" );
221     }
222
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 {
231         w.write( "<" );
232         w.write( element_name );
233         if ( !ForesterUtil.isEmpty( attribute1_value ) ) {
234             w.write( " " );
235             w.write( attribute1_name );
236             w.write( "=\"" );
237             w.write( attribute1_value );
238             w.write( "\"" );
239         }
240         if ( !ForesterUtil.isEmpty( attribute2_value ) ) {
241             w.write( " " );
242             w.write( attribute2_name );
243             w.write( "=\"" );
244             w.write( attribute2_value );
245             w.write( "\"" );
246         }
247         if ( !ForesterUtil.isEmpty( attribute2_value ) ) {
248             w.write( " " );
249             w.write( attribute3_name );
250             w.write( "=\"" );
251             w.write( attribute3_value );
252             w.write( "\"" );
253         }
254         w.write( ">" );
255     }
256
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 {
267         w.write( "<" );
268         w.write( element_name );
269         if ( !ForesterUtil.isEmpty( attribute1_value ) ) {
270             w.write( " " );
271             w.write( attribute1_name );
272             w.write( "=\"" );
273             w.write( attribute1_value );
274             w.write( "\"" );
275         }
276         if ( !ForesterUtil.isEmpty( attribute2_value ) ) {
277             w.write( " " );
278             w.write( attribute2_name );
279             w.write( "=\"" );
280             w.write( attribute2_value );
281             w.write( "\"" );
282         }
283         if ( !ForesterUtil.isEmpty( attribute3_value ) ) {
284             w.write( " " );
285             w.write( attribute3_name );
286             w.write( "=\"" );
287             w.write( attribute3_value );
288             w.write( "\"" );
289         }
290         if ( !ForesterUtil.isEmpty( attribute4_value ) ) {
291             w.write( " " );
292             w.write( attribute4_name );
293             w.write( "=\"" );
294             w.write( attribute4_value );
295             w.write( "\"" );
296         }
297         w.write( ">" );
298     }
299
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 {
312         w.write( "<" );
313         w.write( element_name );
314         if ( !ForesterUtil.isEmpty( attribute1_value ) ) {
315             w.write( " " );
316             w.write( attribute1_name );
317             w.write( "=\"" );
318             w.write( attribute1_value );
319             w.write( "\"" );
320         }
321         if ( !ForesterUtil.isEmpty( attribute2_value ) ) {
322             w.write( " " );
323             w.write( attribute2_name );
324             w.write( "=\"" );
325             w.write( attribute2_value );
326             w.write( "\"" );
327         }
328         if ( !ForesterUtil.isEmpty( attribute3_value ) ) {
329             w.write( " " );
330             w.write( attribute3_name );
331             w.write( "=\"" );
332             w.write( attribute3_value );
333             w.write( "\"" );
334         }
335         if ( !ForesterUtil.isEmpty( attribute4_value ) ) {
336             w.write( " " );
337             w.write( attribute4_name );
338             w.write( "=\"" );
339             w.write( attribute4_value );
340             w.write( "\"" );
341         }
342         if ( !ForesterUtil.isEmpty( attribute5_value ) ) {
343             w.write( " " );
344             w.write( attribute5_name );
345             w.write( "=\"" );
346             w.write( attribute5_value );
347             w.write( "\"" );
348         }
349         w.write( ">" );
350     }
351
352     /**
353      * Creates a deep copy of ArrayList of PhylogenyData objects.
354      * 
355      * @param list
356      *            an ArrayList of PhylogenyData objects
357      * @return a deep copy of ArrayList list
358      */
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() );
363         }
364         return l;
365     }
366
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 ) );
372     }
373
374     public static String replaceIllegalXmlCharacters( final String value ) {
375         String v = value.replaceAll( "&", "&amp;" );
376         v = v.replaceAll( "<", "&lt;" );
377         v = v.replaceAll( ">", "&gt;" );
378         v = v.replaceAll( "'", "&apos;" );
379         v = v.replaceAll( "\"", "&quot;" );
380         return v;
381     }
382 }