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: www.phylosoft.org/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 public static void appendClose( final Writer w, final String element_name ) throws IOException {
40 w.write( element_name );
44 public static void appendElement( final Writer w, final String element_name, final String value )
46 appendOpen( w, element_name );
47 w.write( replaceIllegalXmlCharacters( value ) );
48 appendClose( w, element_name );
51 public static void appendElement( final Writer w,
52 final String element_name,
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 );
62 public static void appendElement( final Writer w,
63 final String element_name,
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 );
72 public static void appendElement( final Writer w,
73 final String element_name,
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 );
86 public static void appendElement( final Writer w,
87 final String element_name,
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 );
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 );
125 appendClose( w, element_name );
128 public static void appendElement( final Writer w,
129 final String element_name,
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 );
157 w.write( replaceIllegalXmlCharacters( value ) );
158 appendClose( w, element_name );
161 public static void appendOpen( final Writer w, final String element_name ) throws IOException {
163 w.write( element_name );
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 {
172 w.write( element_name );
173 if ( !ForesterUtil.isEmpty( attribute_value ) ) {
175 w.write( attribute_name );
177 w.write( attribute_value );
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 {
190 w.write( element_name );
191 if ( !ForesterUtil.isEmpty( attribute1_value ) ) {
193 w.write( attribute1_name );
195 w.write( attribute1_value );
198 if ( !ForesterUtil.isEmpty( attribute2_value ) ) {
200 w.write( attribute2_name );
202 w.write( attribute2_value );
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 {
217 w.write( element_name );
218 if ( !ForesterUtil.isEmpty( attribute1_value ) ) {
220 w.write( attribute1_name );
222 w.write( attribute1_value );
225 if ( !ForesterUtil.isEmpty( attribute2_value ) ) {
227 w.write( attribute2_name );
229 w.write( attribute2_value );
232 if ( !ForesterUtil.isEmpty( attribute2_value ) ) {
234 w.write( attribute3_name );
236 w.write( attribute3_value );
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 {
253 w.write( element_name );
254 if ( !ForesterUtil.isEmpty( attribute1_value ) ) {
256 w.write( attribute1_name );
258 w.write( attribute1_value );
261 if ( !ForesterUtil.isEmpty( attribute2_value ) ) {
263 w.write( attribute2_name );
265 w.write( attribute2_value );
268 if ( !ForesterUtil.isEmpty( attribute3_value ) ) {
270 w.write( attribute3_name );
272 w.write( attribute3_value );
275 if ( !ForesterUtil.isEmpty( attribute4_value ) ) {
277 w.write( attribute4_name );
279 w.write( attribute4_value );
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 {
298 w.write( element_name );
299 if ( !ForesterUtil.isEmpty( attribute1_value ) ) {
301 w.write( attribute1_name );
303 w.write( attribute1_value );
306 if ( !ForesterUtil.isEmpty( attribute2_value ) ) {
308 w.write( attribute2_name );
310 w.write( attribute2_value );
313 if ( !ForesterUtil.isEmpty( attribute3_value ) ) {
315 w.write( attribute3_name );
317 w.write( attribute3_value );
320 if ( !ForesterUtil.isEmpty( attribute4_value ) ) {
322 w.write( attribute4_name );
324 w.write( attribute4_value );
327 if ( !ForesterUtil.isEmpty( attribute5_value ) ) {
329 w.write( attribute5_name );
331 w.write( attribute5_value );
338 * Creates a deep copy of ArrayList of PhylogenyData objects.
341 * an ArrayList of PhylogenyData objects
342 * @return a deep copy of ArrayList list
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() );
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 ) );
359 public static void drawString( final String str, final double x, final double y, final Graphics g ) {
361 org.forester.util.ForesterUtil.roundToInt( x ),
362 org.forester.util.ForesterUtil.roundToInt( y ) );
365 public static String replaceIllegalXmlCharacters( final String value ) {
366 String v = value.replaceAll( "&", "&" );
367 v = v.replaceAll( "<", "<" );
368 v = v.replaceAll( ">", ">" );
369 v = v.replaceAll( "'", "'" );
370 v = v.replaceAll( "\"", """ );