f430477aa8598efac8b5be17ca97daf1b245d5e4
[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: www.phylosoft.org/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     public static void appendClose( final Writer w, final String element_name ) throws IOException {
39         w.write( "</" );
40         w.write( element_name );
41         w.write( ">" );
42     }
43
44     public static void appendElement( final Writer w, final String element_name, final String value )
45             throws IOException {
46         appendOpen( w, element_name );
47         w.write( replaceIllegalXmlCharacters( value ) );
48         appendClose( w, element_name );
49     }
50
51     public static void appendElement( final Writer w,
52                                       final String element_name,
53                                       final String value,
54                                       final String indentation ) throws IOException {
55         w.write( ForesterUtil.LINE_SEPARATOR );
56         w.write( indentation );
57         w.write( PhylogenyWriter.PHYLO_XML_INTENDATION_BASE );
58         // Something like this replacement needs to be done in a more systematic manner.
59         appendElement( w, element_name, value );
60     }
61
62     public static void appendElement( final Writer w,
63                                       final String element_name,
64                                       final String value,
65                                       final String attribute_name,
66                                       final String attribute_value ) throws IOException {
67         appendOpen( w, element_name, attribute_name, attribute_value );
68         w.write( replaceIllegalXmlCharacters( value ) );
69         appendClose( w, element_name );
70     }
71
72     public static void appendElement( final Writer w,
73                                       final String element_name,
74                                       final String value,
75                                       final String attribute_name,
76                                       final String attribute_value,
77                                       final String indentation ) throws IOException {
78         w.write( ForesterUtil.LINE_SEPARATOR );
79         w.write( indentation );
80         w.write( PhylogenyWriter.PHYLO_XML_INTENDATION_BASE );
81         appendOpen( w, element_name, attribute_name, attribute_value );
82         w.write( replaceIllegalXmlCharacters( value ) );
83         appendClose( w, element_name );
84     }
85
86     public static void appendElement( final Writer w,
87                                       final String element_name,
88                                       final String value,
89                                       final String attribute1_name,
90                                       final String attribute1_value,
91                                       final String attribute2_name,
92                                       final String attribute2_value,
93                                       final String indentation ) throws IOException {
94         w.write( ForesterUtil.LINE_SEPARATOR );
95         w.write( indentation );
96         w.write( PhylogenyWriter.PHYLO_XML_INTENDATION_BASE );
97         appendOpen( w, element_name, attribute1_name, attribute1_value, attribute2_name, attribute2_value );
98         w.write( replaceIllegalXmlCharacters( value ) );
99         appendClose( w, element_name );
100     }
101
102     public static void appendElement( final Writer w,
103                                       final String element_name,
104                                       final String attribute1_name,
105                                       final String attribute1_value,
106                                       final String attribute2_name,
107                                       final String attribute2_value,
108                                       final String attribute3_name,
109                                       final String attribute3_value,
110                                       final String attribute4_name,
111                                       final String attribute4_value,
112                                       final String indentation ) throws IOException {
113         w.write( ForesterUtil.LINE_SEPARATOR );
114         w.write( indentation );
115         appendOpen( w,
116                     element_name,
117                     attribute1_name,
118                     attribute1_value,
119                     attribute2_name,
120                     attribute2_value,
121                     attribute3_name,
122                     attribute3_value,
123                     attribute4_name,
124                     attribute4_value );
125         appendClose( w, element_name );
126     }
127
128     public static void appendElement( final Writer w,
129                                       final String element_name,
130                                       final String value,
131                                       final String attribute1_name,
132                                       final String attribute1_value,
133                                       final String attribute2_name,
134                                       final String attribute2_value,
135                                       final String attribute3_name,
136                                       final String attribute3_value,
137                                       final String attribute4_name,
138                                       final String attribute4_value,
139                                       final String attribute5_name,
140                                       final String attribute5_value,
141                                       final String indentation ) throws IOException {
142         w.write( ForesterUtil.LINE_SEPARATOR );
143         w.write( indentation );
144         w.write( PhylogenyWriter.PHYLO_XML_INTENDATION_BASE );
145         appendOpen( w,
146                     element_name,
147                     attribute1_name,
148                     attribute1_value,
149                     attribute2_name,
150                     attribute2_value,
151                     attribute3_name,
152                     attribute3_value,
153                     attribute4_name,
154                     attribute4_value,
155                     attribute5_name,
156                     attribute5_value );
157         w.write( replaceIllegalXmlCharacters( value ) );
158         appendClose( w, element_name );
159     }
160
161     public static void appendOpen( final Writer w, final String element_name ) throws IOException {
162         w.write( "<" );
163         w.write( element_name );
164         w.write( ">" );
165     }
166
167     public static void appendOpen( final Writer w,
168                                    final String element_name,
169                                    final String attribute_name,
170                                    final String attribute_value ) throws IOException {
171         w.write( "<" );
172         w.write( element_name );
173         if ( !ForesterUtil.isEmpty( attribute_value ) ) {
174             w.write( " " );
175             w.write( attribute_name );
176             w.write( "=\"" );
177             w.write( attribute_value );
178             w.write( "\"" );
179         }
180         w.write( ">" );
181     }
182
183     public static void appendOpen( final Writer w,
184                                    final String element_name,
185                                    final String attribute1_name,
186                                    final String attribute1_value,
187                                    final String attribute2_name,
188                                    final String attribute2_value ) throws IOException {
189         w.write( "<" );
190         w.write( element_name );
191         if ( !ForesterUtil.isEmpty( attribute1_value ) ) {
192             w.write( " " );
193             w.write( attribute1_name );
194             w.write( "=\"" );
195             w.write( attribute1_value );
196             w.write( "\"" );
197         }
198         if ( !ForesterUtil.isEmpty( attribute2_value ) ) {
199             w.write( " " );
200             w.write( attribute2_name );
201             w.write( "=\"" );
202             w.write( attribute2_value );
203             w.write( "\"" );
204         }
205         w.write( ">" );
206     }
207
208     public static void appendOpen( final Writer w,
209                                    final String element_name,
210                                    final String attribute1_name,
211                                    final String attribute1_value,
212                                    final String attribute2_name,
213                                    final String attribute2_value,
214                                    final String attribute3_name,
215                                    final String attribute3_value ) throws IOException {
216         w.write( "<" );
217         w.write( element_name );
218         if ( !ForesterUtil.isEmpty( attribute1_value ) ) {
219             w.write( " " );
220             w.write( attribute1_name );
221             w.write( "=\"" );
222             w.write( attribute1_value );
223             w.write( "\"" );
224         }
225         if ( !ForesterUtil.isEmpty( attribute2_value ) ) {
226             w.write( " " );
227             w.write( attribute2_name );
228             w.write( "=\"" );
229             w.write( attribute2_value );
230             w.write( "\"" );
231         }
232         if ( !ForesterUtil.isEmpty( attribute2_value ) ) {
233             w.write( " " );
234             w.write( attribute3_name );
235             w.write( "=\"" );
236             w.write( attribute3_value );
237             w.write( "\"" );
238         }
239         w.write( ">" );
240     }
241
242     public static void appendOpen( final Writer w,
243                                    final String element_name,
244                                    final String attribute1_name,
245                                    final String attribute1_value,
246                                    final String attribute2_name,
247                                    final String attribute2_value,
248                                    final String attribute3_name,
249                                    final String attribute3_value,
250                                    final String attribute4_name,
251                                    final String attribute4_value ) throws IOException {
252         w.write( "<" );
253         w.write( element_name );
254         if ( !ForesterUtil.isEmpty( attribute1_value ) ) {
255             w.write( " " );
256             w.write( attribute1_name );
257             w.write( "=\"" );
258             w.write( attribute1_value );
259             w.write( "\"" );
260         }
261         if ( !ForesterUtil.isEmpty( attribute2_value ) ) {
262             w.write( " " );
263             w.write( attribute2_name );
264             w.write( "=\"" );
265             w.write( attribute2_value );
266             w.write( "\"" );
267         }
268         if ( !ForesterUtil.isEmpty( attribute3_value ) ) {
269             w.write( " " );
270             w.write( attribute3_name );
271             w.write( "=\"" );
272             w.write( attribute3_value );
273             w.write( "\"" );
274         }
275         if ( !ForesterUtil.isEmpty( attribute4_value ) ) {
276             w.write( " " );
277             w.write( attribute4_name );
278             w.write( "=\"" );
279             w.write( attribute4_value );
280             w.write( "\"" );
281         }
282         w.write( ">" );
283     }
284
285     public static void appendOpen( final Writer w,
286                                    final String element_name,
287                                    final String attribute1_name,
288                                    final String attribute1_value,
289                                    final String attribute2_name,
290                                    final String attribute2_value,
291                                    final String attribute3_name,
292                                    final String attribute3_value,
293                                    final String attribute4_name,
294                                    final String attribute4_value,
295                                    final String attribute5_name,
296                                    final String attribute5_value ) throws IOException {
297         w.write( "<" );
298         w.write( element_name );
299         if ( !ForesterUtil.isEmpty( attribute1_value ) ) {
300             w.write( " " );
301             w.write( attribute1_name );
302             w.write( "=\"" );
303             w.write( attribute1_value );
304             w.write( "\"" );
305         }
306         if ( !ForesterUtil.isEmpty( attribute2_value ) ) {
307             w.write( " " );
308             w.write( attribute2_name );
309             w.write( "=\"" );
310             w.write( attribute2_value );
311             w.write( "\"" );
312         }
313         if ( !ForesterUtil.isEmpty( attribute3_value ) ) {
314             w.write( " " );
315             w.write( attribute3_name );
316             w.write( "=\"" );
317             w.write( attribute3_value );
318             w.write( "\"" );
319         }
320         if ( !ForesterUtil.isEmpty( attribute4_value ) ) {
321             w.write( " " );
322             w.write( attribute4_name );
323             w.write( "=\"" );
324             w.write( attribute4_value );
325             w.write( "\"" );
326         }
327         if ( !ForesterUtil.isEmpty( attribute5_value ) ) {
328             w.write( " " );
329             w.write( attribute5_name );
330             w.write( "=\"" );
331             w.write( attribute5_value );
332             w.write( "\"" );
333         }
334         w.write( ">" );
335     }
336
337     /**
338      * Creates a deep copy of ArrayList of PhylogenyData objects.
339      * 
340      * @param list
341      *            an ArrayList of PhylogenyData objects
342      * @return a deep copy of ArrayList list
343      */
344     public static ArrayList<PhylogenyData> copy( final ArrayList<PhylogenyData> list ) {
345         final ArrayList<PhylogenyData> l = new ArrayList<PhylogenyData>( list.size() );
346         for( int i = 0; i < list.size(); ++i ) {
347             l.add( ( list.get( i ) ).copy() );
348         }
349         return l;
350     }
351
352     public static void drawLine( final double x1, final double y1, final double x2, final double y2, final Graphics g ) {
353         g.drawLine( org.forester.util.ForesterUtil.roundToInt( x1 ),
354                     org.forester.util.ForesterUtil.roundToInt( y1 ),
355                     org.forester.util.ForesterUtil.roundToInt( x2 ),
356                     org.forester.util.ForesterUtil.roundToInt( y2 ) );
357     }
358
359     public static void drawString( final String str, final double x, final double y, final Graphics g ) {
360         g.drawString( str,
361                       org.forester.util.ForesterUtil.roundToInt( x ),
362                       org.forester.util.ForesterUtil.roundToInt( y ) );
363     }
364
365     public static String replaceIllegalXmlCharacters( final String value ) {
366         String v = value.replaceAll( "&", "&amp;" );
367         v = v.replaceAll( "<", "&lt;" );
368         v = v.replaceAll( ">", "&gt;" );
369         v = v.replaceAll( "'", "&apos;" );
370         v = v.replaceAll( "\"", "&quot;" );
371         return v;
372     }
373 }