KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > jcharts > test > HTMLGenerator


1 /***********************************************************************************************
2  * File: HTMLGenerator.java
3  * Last Modified: $Id: HTMLGenerator.java,v 1.1 2003/05/17 17:01:11 nathaniel_auvil Exp $
4  * Copyright (C) 2000
5  * Author: Nathaniel G. Auvil
6  * Contributor(s):
7  *
8  * Copyright 2002 (C) Nathaniel G. Auvil. All Rights Reserved.
9  *
10  * Redistribution and use of this software and associated documentation
11  * ("Software"), with or without modification, are permitted provided
12  * that the following conditions are met:
13  *
14  * 1. Redistributions of source code must retain copyright
15  * statements and notices. Redistributions must also contain a
16  * copy of this document.
17  *
18  * 2. Redistributions in binary form must reproduce the
19  * above copyright notice, this list of conditions and the
20  * following disclaimer in the documentation and/or other
21  * materials provided with the distribution.
22  *
23  * 3. The name "jCharts" or "Nathaniel G. Auvil" must not be used to
24  * endorse or promote products derived from this Software without
25  * prior written permission of Nathaniel G. Auvil. For written
26  * permission, please contact nathaniel_auvil@users.sourceforge.net
27  *
28  * 4. Products derived from this Software may not be called "jCharts"
29  * nor may "jCharts" appear in their names without prior written
30  * permission of Nathaniel G. Auvil. jCharts is a registered
31  * trademark of Nathaniel G. Auvil.
32  *
33  * 5. Due credit should be given to the jCharts Project
34  * (http://jcharts.sourceforge.net/).
35  *
36  * THIS SOFTWARE IS PROVIDED BY Nathaniel G. Auvil AND CONTRIBUTORS
37  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
38  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
39  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
40  * jCharts OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
41  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
42  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
43  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47  * OF THE POSSIBILITY OF SUCH DAMAGE.
48  ************************************************************************************************/

49
50
51 package org.krysalis.jcharts.test;
52
53
54 import org.krysalis.jcharts.imageMap.ImageMapArea;
55 import org.krysalis.jcharts.imageMap.ImageMap;
56
57 import java.io.FileWriter JavaDoc;
58 import java.util.Iterator JavaDoc;
59
60
61 /*********************************************************************************************
62  * Utility class for viewing a series of tests.
63  *
64  **********************************************************************************************/

65 final public class HTMLGenerator
66 {
67     private String JavaDoc fileName;
68     private StringBuffer JavaDoc stringBuffer;
69
70
71     /*****************************************************************************************
72      *
73      * @param fileName the name of the file to write to.
74      ******************************************************************************************/

75     public HTMLGenerator( String JavaDoc fileName )
76     {
77         this.fileName = fileName;
78         this.stringBuffer = new StringBuffer JavaDoc( 1024 );
79         this.stringBuffer.append( "<HTML><BODY>" );
80     }
81
82
83     /*****************************************************************************************
84      * Adds passed field to table. Use reflection to get the fields.
85      *
86      * @param name
87      * @param object
88      ******************************************************************************************/

89     public void addField( String JavaDoc name, Object JavaDoc object )
90     {
91         if( object instanceof boolean[] )
92         {
93             this.addTableRow( name, HTMLGenerator.arrayToString( ( boolean[] ) object ) );
94         }
95         else if( object instanceof int[] )
96         {
97             this.addTableRow( name, HTMLGenerator.arrayToString( ( int[] ) object ) );
98         }
99         else if( object instanceof double[] )
100         {
101             this.addTableRow( name, HTMLGenerator.arrayToString( ( double[] ) object ) );
102         }
103         else if( object instanceof float[] )
104         {
105             this.addTableRow( name, HTMLGenerator.arrayToString( ( float[] ) object ) );
106         }
107         else if( object instanceof Object JavaDoc[] )
108         {
109             this.addTableRow( name, HTMLGenerator.arrayToString( ( Object JavaDoc[] ) object ) );
110         }
111         else
112         {
113             this.addTableRow( name, object );
114         }
115     }
116
117
118     /*****************************************************************************************
119      * Adds a String
120      *
121      ******************************************************************************************/

122     public void addString( Object JavaDoc object )
123     {
124         this.stringBuffer.append( ( object != null ) ? object.toString() : "NULL" );
125     }
126
127
128     /*****************************************************************************************
129      * Adds a String
130      *
131      ******************************************************************************************/

132     public void addString( String JavaDoc label, Object JavaDoc object )
133     {
134         this.addString( "<B>" );
135         this.stringBuffer.append( label );
136         this.addString( "</B>" );
137         this.stringBuffer.append( object.toString() );
138     }
139
140
141     /*****************************************************************************************
142      * Adds an Array
143      *
144      * @param object
145      ******************************************************************************************/

146     public static String JavaDoc arrayToString( Object JavaDoc[] object )
147     {
148         if( object == null )
149         {
150             return null;
151         }
152
153         StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc( 200 );
154         for( int i = 0; i < object.length; i++ )
155         {
156             stringBuffer.append( object[ i ].toString() );
157             if( i < object.length - 1 )
158             {
159                 stringBuffer.append( ", " );
160             }
161         }
162         return stringBuffer.toString();
163     }
164
165
166     /*****************************************************************************************
167      * Adds an Array
168      *
169      * @param array
170      ******************************************************************************************/

171     public static String JavaDoc arrayToString( boolean[] array )
172     {
173         StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc( 100 );
174         for( int i = 0; i < array.length; i++ )
175         {
176             stringBuffer.append( new Boolean JavaDoc( array[ i ] ).toString() );
177             if( i < array.length - 1 )
178             {
179                 stringBuffer.append( ", " );
180             }
181         }
182         return stringBuffer.toString();
183     }
184
185
186     /*****************************************************************************************
187      * Adds an Array
188      *
189      * @param values
190      ******************************************************************************************/

191     public static String JavaDoc arrayToString( double[] values )
192     {
193         StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc( 200 );
194         for( int i = 0; i < values.length; i++ )
195         {
196             stringBuffer.append( Double.toString( values[ i ] ) );
197             if( i < values.length - 1 )
198             {
199                 stringBuffer.append( ", " );
200             }
201         }
202         return stringBuffer.toString();
203     }
204
205
206     /*****************************************************************************************
207      * Adds an Array
208      *
209      * @param values
210      ******************************************************************************************/

211     public static String JavaDoc arrayToString( double[][] values )
212     {
213         StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc( 400 );
214         for( int i = 0; i < values.length; i++ )
215         {
216             stringBuffer.append( " { " );
217
218             for( int j = 0; j < values[ 0 ].length; j++ )
219             {
220                 stringBuffer.append( values[ i ][ j ] );
221                 if( j < values[ 0 ].length - 1 )
222                 {
223                     stringBuffer.append( ", " );
224                 }
225             }
226
227             stringBuffer.append( " }<BR> " );
228         }
229         return stringBuffer.toString();
230     }
231
232
233     /*****************************************************************************************
234      * Adds an Array
235      *
236      * @param values
237      ******************************************************************************************/

238     public static String JavaDoc arrayToString( float[] values )
239     {
240         StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc( 200 );
241         for( int i = 0; i < values.length; i++ )
242         {
243             stringBuffer.append( Float.toString( values[ i ] ) );
244             if( i < values.length - 1 )
245             {
246                 stringBuffer.append( ", " );
247             }
248         }
249         return stringBuffer.toString();
250     }
251
252
253     /*****************************************************************************************
254      * Adds an Array
255      *
256      * @param values
257      ******************************************************************************************/

258     public static String JavaDoc arrayToString( int[] values )
259     {
260         StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc( 200 );
261         for( int i = 0; i < values.length; i++ )
262         {
263             stringBuffer.append( Integer.toString( values[ i ] ) );
264             if( i < values.length - 1 )
265             {
266                 stringBuffer.append( ", " );
267             }
268         }
269         return stringBuffer.toString();
270     }
271
272
273     /*****************************************************************************************
274      * Adds an image
275      *
276      ******************************************************************************************/

277     public void addImage( String JavaDoc fileName, ImageMap imageMap )
278     {
279         this.stringBuffer.append( "<img SRC=\"" );
280         this.stringBuffer.append( fileName );
281         this.stringBuffer.append( "\"" );
282
283         if( imageMap != null )
284         {
285             this.stringBuffer.append( " useMap=\"#" );
286             this.stringBuffer.append( fileName );
287             this.stringBuffer.append( "\"" );
288         }
289
290         this.stringBuffer.append( ">" );
291
292         if( imageMap != null )
293         {
294             this.addImageMapData( imageMap, fileName );
295         }
296     }
297
298
299     private void addImageMapData( ImageMap imageMap, String JavaDoc fileName )
300     {
301         this.stringBuffer.append( "<map name=\"" );
302         this.stringBuffer.append( fileName );
303         this.stringBuffer.append( "\">" );
304
305         Iterator JavaDoc iterator = imageMap.getIterator();
306         while( iterator.hasNext() )
307         {
308             ImageMapArea imageMapArea = ( ImageMapArea ) iterator.next();
309
310             StringBuffer JavaDoc html = new StringBuffer JavaDoc( 50 );
311             html.append( "href=\"javascript:alert( 'value= " );
312             html.append( imageMapArea.getValue() );
313             html.append( ", legend label= " );
314             html.append( imageMapArea.getLengendLabel() );
315             html.append( ", axis label= " );
316             html.append( imageMapArea.getXAxisLabel() );
317             html.append( "');\"" );
318
319             this.stringBuffer.append( imageMapArea.toHTML( html.toString() ) );
320         }
321
322         this.stringBuffer.append( "</map>" );
323     }
324
325
326     /*****************************************************************************************
327      * Add line break
328      *
329      ******************************************************************************************/

330     public void addLineBreak()
331     {
332         this.stringBuffer.append( "<BR>" );
333     }
334
335
336     /*****************************************************************************************
337      * Writes the file.
338      *
339      ******************************************************************************************/

340     public void saveFile()
341     {
342         this.stringBuffer.append( "</BODY></HTML>" );
343
344         try
345         {
346             FileWriter JavaDoc fileWriter = new FileWriter JavaDoc( this.fileName );
347             fileWriter.write( this.stringBuffer.toString() );
348             fileWriter.flush();
349             fileWriter.close();
350         }
351         catch( Throwable JavaDoc throwable )
352         {
353             throwable.printStackTrace();
354         }
355     }
356
357
358     /*****************************************************************************************
359      *
360      * @param label
361      * @param value
362      ******************************************************************************************/

363     public void addTableRow( String JavaDoc label, Object JavaDoc value )
364     {
365         this.addString( "<TR><TD NOWRAP BGCOLOR=#FFFFFF>" );
366         this.addString( label );
367         this.addString( "</TD><TD NOWRAP BGCOLOR=#FFFFFF>" );
368         this.addString( value );
369         this.addString( "</TD></TR>" );
370     }
371
372
373     /*****************************************************************************************
374      *
375      * @param propertiesName
376      ******************************************************************************************/

377     public void propertiesTableStart( String JavaDoc propertiesName )
378     {
379         this.addString( "<TABLE BGCOLOR=#000000 BORDER=0 CELLSPACING=1 CELLPADDING=3>" );
380         this.addString( "<TR><TD BGCOLOR=#D0FBCE COLSPAN=2><B>" + propertiesName + "</B></TD></TR>" );
381     }
382
383
384     public void propertiesTableEnd()
385     {
386         this.addString( "</TABLE>" );
387     }
388
389
390     public void propertiesTableRowStart()
391     {
392         this.addString( "<TR><TD WIDTH=100% BGCOLOR=#AAAAAA>" );
393     }
394
395
396     public void propertiesTableRowEnd()
397     {
398         this.addString( "</TD></TR>" );
399     }
400
401
402     /*****************************************************************************************
403      *
404      * @param chartName
405      * @param imageFileName
406      * @param imageMap if this is NULL we are not creating image map data in html
407      ******************************************************************************************/

408     public void chartTableStart( String JavaDoc chartName, String JavaDoc imageFileName, ImageMap imageMap )
409     {
410         this.addString( "<TABLE BGCOLOR=#000000 BORDER=0 CELLSPACING=1 CELLPADDING=3>" );
411         this.addString( "<TR><TD BGCOLOR=#FDFEC2 COLSPAN=1><B>" + chartName + "</B></TD></TR>" );
412         this.addString( "<TR><TD WIDTH=100% BGCOLOR=#AAAAAA>" );
413         this.addImage( imageFileName, imageMap );
414         this.addString( "</TD></TR>" );
415     }
416
417
418     public void chartTableEnd()
419     {
420         this.addString( "</TABLE>" );
421     }
422
423
424     public void chartTableRowStart()
425     {
426         this.addString( "<TR><TD WIDTH=100% BGCOLOR=#AAAAAA>" );
427     }
428
429
430     public void chartTableRowEnd()
431     {
432         this.addString( "</TD></TR>" );
433     }
434
435
436     /*****************************************************************************************
437      *
438      ******************************************************************************************/

439     public void legendTableStart()
440     {
441         this.addString( "<TABLE BGCOLOR=#000000 BORDER=0 CELLSPACING=1 CELLPADDING=3>" );
442         this.addString( "<TR><TD BGCOLOR=#FDFEC2 COLSPAN=2><B>Legend</B></TD></TR>" );
443     }
444
445
446     public void legendTableEnd()
447     {
448         this.addString( "</TABLE>" );
449     }
450
451
452     public void innerTableRowStart()
453     {
454         this.addString( "<TR><TD WIDTH=100% COLSPAN=2 BGCOLOR=#777777>" );
455     }
456
457
458     public void innerTableRowEnd()
459     {
460         this.addString( "</TD></TR>" );
461     }
462
463
464 }
Popular Tags