001: /***********************************************************************************************
002: * File: HTMLGenerator.java
003: * Last Modified: $Id: HTMLGenerator.java,v 1.1 2003/05/17 17:01:11 nathaniel_auvil Exp $
004: * Copyright (C) 2000
005: * Author: Nathaniel G. Auvil
006: * Contributor(s):
007: *
008: * Copyright 2002 (C) Nathaniel G. Auvil. All Rights Reserved.
009: *
010: * Redistribution and use of this software and associated documentation
011: * ("Software"), with or without modification, are permitted provided
012: * that the following conditions are met:
013: *
014: * 1. Redistributions of source code must retain copyright
015: * statements and notices. Redistributions must also contain a
016: * copy of this document.
017: *
018: * 2. Redistributions in binary form must reproduce the
019: * above copyright notice, this list of conditions and the
020: * following disclaimer in the documentation and/or other
021: * materials provided with the distribution.
022: *
023: * 3. The name "jCharts" or "Nathaniel G. Auvil" must not be used to
024: * endorse or promote products derived from this Software without
025: * prior written permission of Nathaniel G. Auvil. For written
026: * permission, please contact nathaniel_auvil@users.sourceforge.net
027: *
028: * 4. Products derived from this Software may not be called "jCharts"
029: * nor may "jCharts" appear in their names without prior written
030: * permission of Nathaniel G. Auvil. jCharts is a registered
031: * trademark of Nathaniel G. Auvil.
032: *
033: * 5. Due credit should be given to the jCharts Project
034: * (http://jcharts.sourceforge.net/).
035: *
036: * THIS SOFTWARE IS PROVIDED BY Nathaniel G. Auvil AND CONTRIBUTORS
037: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
038: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
039: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
040: * jCharts OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
041: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
042: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
043: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
044: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
045: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
046: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
047: * OF THE POSSIBILITY OF SUCH DAMAGE.
048: ************************************************************************************************/package org.krysalis.jcharts.test;
049:
050: import org.krysalis.jcharts.imageMap.ImageMapArea;
051: import org.krysalis.jcharts.imageMap.ImageMap;
052:
053: import java.io.FileWriter;
054: import java.util.Iterator;
055:
056: /*********************************************************************************************
057: * Utility class for viewing a series of tests.
058: *
059: **********************************************************************************************/
060: final public class HTMLGenerator {
061: private String fileName;
062: private StringBuffer stringBuffer;
063:
064: /*****************************************************************************************
065: *
066: * @param fileName the name of the file to write to.
067: ******************************************************************************************/
068: public HTMLGenerator(String fileName) {
069: this .fileName = fileName;
070: this .stringBuffer = new StringBuffer(1024);
071: this .stringBuffer.append("<HTML><BODY>");
072: }
073:
074: /*****************************************************************************************
075: * Adds passed field to table. Use reflection to get the fields.
076: *
077: * @param name
078: * @param object
079: ******************************************************************************************/
080: public void addField(String name, Object object) {
081: if (object instanceof boolean[]) {
082: this .addTableRow(name, HTMLGenerator
083: .arrayToString((boolean[]) object));
084: } else if (object instanceof int[]) {
085: this .addTableRow(name, HTMLGenerator
086: .arrayToString((int[]) object));
087: } else if (object instanceof double[]) {
088: this .addTableRow(name, HTMLGenerator
089: .arrayToString((double[]) object));
090: } else if (object instanceof float[]) {
091: this .addTableRow(name, HTMLGenerator
092: .arrayToString((float[]) object));
093: } else if (object instanceof Object[]) {
094: this .addTableRow(name, HTMLGenerator
095: .arrayToString((Object[]) object));
096: } else {
097: this .addTableRow(name, object);
098: }
099: }
100:
101: /*****************************************************************************************
102: * Adds a String
103: *
104: ******************************************************************************************/
105: public void addString(Object object) {
106: this .stringBuffer.append((object != null) ? object.toString()
107: : "NULL");
108: }
109:
110: /*****************************************************************************************
111: * Adds a String
112: *
113: ******************************************************************************************/
114: public void addString(String label, Object object) {
115: this .addString("<B>");
116: this .stringBuffer.append(label);
117: this .addString("</B>");
118: this .stringBuffer.append(object.toString());
119: }
120:
121: /*****************************************************************************************
122: * Adds an Array
123: *
124: * @param object
125: ******************************************************************************************/
126: public static String arrayToString(Object[] object) {
127: if (object == null) {
128: return null;
129: }
130:
131: StringBuffer stringBuffer = new StringBuffer(200);
132: for (int i = 0; i < object.length; i++) {
133: stringBuffer.append(object[i].toString());
134: if (i < object.length - 1) {
135: stringBuffer.append(", ");
136: }
137: }
138: return stringBuffer.toString();
139: }
140:
141: /*****************************************************************************************
142: * Adds an Array
143: *
144: * @param array
145: ******************************************************************************************/
146: public static String arrayToString(boolean[] array) {
147: StringBuffer stringBuffer = new StringBuffer(100);
148: for (int i = 0; i < array.length; i++) {
149: stringBuffer.append(new Boolean(array[i]).toString());
150: if (i < array.length - 1) {
151: stringBuffer.append(", ");
152: }
153: }
154: return stringBuffer.toString();
155: }
156:
157: /*****************************************************************************************
158: * Adds an Array
159: *
160: * @param values
161: ******************************************************************************************/
162: public static String arrayToString(double[] values) {
163: StringBuffer stringBuffer = new StringBuffer(200);
164: for (int i = 0; i < values.length; i++) {
165: stringBuffer.append(Double.toString(values[i]));
166: if (i < values.length - 1) {
167: stringBuffer.append(", ");
168: }
169: }
170: return stringBuffer.toString();
171: }
172:
173: /*****************************************************************************************
174: * Adds an Array
175: *
176: * @param values
177: ******************************************************************************************/
178: public static String arrayToString(double[][] values) {
179: StringBuffer stringBuffer = new StringBuffer(400);
180: for (int i = 0; i < values.length; i++) {
181: stringBuffer.append(" { ");
182:
183: for (int j = 0; j < values[0].length; j++) {
184: stringBuffer.append(values[i][j]);
185: if (j < values[0].length - 1) {
186: stringBuffer.append(", ");
187: }
188: }
189:
190: stringBuffer.append(" }<BR> ");
191: }
192: return stringBuffer.toString();
193: }
194:
195: /*****************************************************************************************
196: * Adds an Array
197: *
198: * @param values
199: ******************************************************************************************/
200: public static String arrayToString(float[] values) {
201: StringBuffer stringBuffer = new StringBuffer(200);
202: for (int i = 0; i < values.length; i++) {
203: stringBuffer.append(Float.toString(values[i]));
204: if (i < values.length - 1) {
205: stringBuffer.append(", ");
206: }
207: }
208: return stringBuffer.toString();
209: }
210:
211: /*****************************************************************************************
212: * Adds an Array
213: *
214: * @param values
215: ******************************************************************************************/
216: public static String arrayToString(int[] values) {
217: StringBuffer stringBuffer = new StringBuffer(200);
218: for (int i = 0; i < values.length; i++) {
219: stringBuffer.append(Integer.toString(values[i]));
220: if (i < values.length - 1) {
221: stringBuffer.append(", ");
222: }
223: }
224: return stringBuffer.toString();
225: }
226:
227: /*****************************************************************************************
228: * Adds an image
229: *
230: ******************************************************************************************/
231: public void addImage(String fileName, ImageMap imageMap) {
232: this .stringBuffer.append("<img src=\"");
233: this .stringBuffer.append(fileName);
234: this .stringBuffer.append("\"");
235:
236: if (imageMap != null) {
237: this .stringBuffer.append(" useMap=\"#");
238: this .stringBuffer.append(fileName);
239: this .stringBuffer.append("\"");
240: }
241:
242: this .stringBuffer.append(">");
243:
244: if (imageMap != null) {
245: this .addImageMapData(imageMap, fileName);
246: }
247: }
248:
249: private void addImageMapData(ImageMap imageMap, String fileName) {
250: this .stringBuffer.append("<map name=\"");
251: this .stringBuffer.append(fileName);
252: this .stringBuffer.append("\">");
253:
254: Iterator iterator = imageMap.getIterator();
255: while (iterator.hasNext()) {
256: ImageMapArea imageMapArea = (ImageMapArea) iterator.next();
257:
258: StringBuffer html = new StringBuffer(50);
259: html.append("href=\"javascript:alert( 'value= ");
260: html.append(imageMapArea.getValue());
261: html.append(", legend label= ");
262: html.append(imageMapArea.getLengendLabel());
263: html.append(", axis label= ");
264: html.append(imageMapArea.getXAxisLabel());
265: html.append("');\"");
266:
267: this .stringBuffer.append(imageMapArea.toHTML(html
268: .toString()));
269: }
270:
271: this .stringBuffer.append("</map>");
272: }
273:
274: /*****************************************************************************************
275: * Add line break
276: *
277: ******************************************************************************************/
278: public void addLineBreak() {
279: this .stringBuffer.append("<BR>");
280: }
281:
282: /*****************************************************************************************
283: * Writes the file.
284: *
285: ******************************************************************************************/
286: public void saveFile() {
287: this .stringBuffer.append("</BODY></HTML>");
288:
289: try {
290: FileWriter fileWriter = new FileWriter(this .fileName);
291: fileWriter.write(this .stringBuffer.toString());
292: fileWriter.flush();
293: fileWriter.close();
294: } catch (Throwable throwable) {
295: throwable.printStackTrace();
296: }
297: }
298:
299: /*****************************************************************************************
300: *
301: * @param label
302: * @param value
303: ******************************************************************************************/
304: public void addTableRow(String label, Object value) {
305: this .addString("<TR><TD NOWRAP BGCOLOR=#FFFFFF>");
306: this .addString(label);
307: this .addString("</TD><TD NOWRAP BGCOLOR=#FFFFFF>");
308: this .addString(value);
309: this .addString("</TD></TR>");
310: }
311:
312: /*****************************************************************************************
313: *
314: * @param propertiesName
315: ******************************************************************************************/
316: public void propertiesTableStart(String propertiesName) {
317: this
318: .addString("<TABLE BGCOLOR=#000000 BORDER=0 CELLSPACING=1 CELLPADDING=3>");
319: this .addString("<TR><TD BGCOLOR=#D0FBCE COLSPAN=2><B>"
320: + propertiesName + "</B></TD></TR>");
321: }
322:
323: public void propertiesTableEnd() {
324: this .addString("</TABLE>");
325: }
326:
327: public void propertiesTableRowStart() {
328: this .addString("<TR><TD WIDTH=100% BGCOLOR=#AAAAAA>");
329: }
330:
331: public void propertiesTableRowEnd() {
332: this .addString("</TD></TR>");
333: }
334:
335: /*****************************************************************************************
336: *
337: * @param chartName
338: * @param imageFileName
339: * @param imageMap if this is NULL we are not creating image map data in html
340: ******************************************************************************************/
341: public void chartTableStart(String chartName, String imageFileName,
342: ImageMap imageMap) {
343: this
344: .addString("<TABLE BGCOLOR=#000000 BORDER=0 CELLSPACING=1 CELLPADDING=3>");
345: this .addString("<TR><TD BGCOLOR=#FDFEC2 COLSPAN=1><B>"
346: + chartName + "</B></TD></TR>");
347: this .addString("<TR><TD WIDTH=100% BGCOLOR=#AAAAAA>");
348: this .addImage(imageFileName, imageMap);
349: this .addString("</TD></TR>");
350: }
351:
352: public void chartTableEnd() {
353: this .addString("</TABLE>");
354: }
355:
356: public void chartTableRowStart() {
357: this .addString("<TR><TD WIDTH=100% BGCOLOR=#AAAAAA>");
358: }
359:
360: public void chartTableRowEnd() {
361: this .addString("</TD></TR>");
362: }
363:
364: /*****************************************************************************************
365: *
366: ******************************************************************************************/
367: public void legendTableStart() {
368: this
369: .addString("<TABLE BGCOLOR=#000000 BORDER=0 CELLSPACING=1 CELLPADDING=3>");
370: this
371: .addString("<TR><TD BGCOLOR=#FDFEC2 COLSPAN=2><B>Legend</B></TD></TR>");
372: }
373:
374: public void legendTableEnd() {
375: this .addString("</TABLE>");
376: }
377:
378: public void innerTableRowStart() {
379: this .addString("<TR><TD WIDTH=100% COLSPAN=2 BGCOLOR=#777777>");
380: }
381:
382: public void innerTableRowEnd() {
383: this .addString("</TD></TR>");
384: }
385:
386: }
|