KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > webman > generator > eventhandler > GRHTMLWrapper


1 package de.webman.generator.eventhandler;
2
3 import java.io.*;
4 import com.teamkonzept.international.LanguageManager;
5
6 /**
7  * Wraps the output of the generator event handlers with styled HTML.
8  * <p>
9  * This is achieved by delegating all stream method calls to the original
10  * stream object while prepending and appending styled HTML.
11  * @author $Author: uli $
12  * @version $Revision: 1.8 $
13  */

14 public class GRHTMLWrapper extends PrintWriter
15 {
16     // Constants
17

18     /**
19      * The OS-dependent line separator.
20      */

21     public static final String JavaDoc LINE_SEPARATOR = System.getProperty("line.separator");
22
23     public static String JavaDoc HEADER_START = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\"><HTML><HEAD>";
24
25     public static String JavaDoc HEADER_META = "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=iso-8859-1\"><META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">";
26
27     public static String JavaDoc HEADER_SCRIPT = "<SCRIPT LANGUAGE=\"JavaScript\">"+ LINE_SEPARATOR + "<!--" +LINE_SEPARATOR +"function omChangeImage(lala,FileName )" + LINE_SEPARATOR + "{ " + LINE_SEPARATOR + "document[lala].src = FileName;}" + LINE_SEPARATOR + "//--></SCRIPT>";
28
29     public static String JavaDoc HEADER_STYLE = "<LINK HREF=\"<TK_APP_CONTEXT>/templates/style.css\" REL=\"stylesheet\" TYPE=\"text/css\"><STYLE TYPE=\"text/css\"><!--BODY {background-color :#336699;}--></STYLE>";
30
31     public static String JavaDoc HEADER_TITLE_START = "<TITLE>";
32
33     public static String JavaDoc HEADER_TITLE_END = "</TITLE>";
34
35     public static String JavaDoc HEADER_END = "</HEAD>";
36
37     public static String JavaDoc BODY_START = "<BODY onLoad=\"self.focus();\" TOPMARGIN=\"0\" LEFTMARGIN=\"0\" MARGINWIDTH=\"0\" MARGINHEIGHT=\"0\">";
38
39     public static String JavaDoc DOCUMENT_PARTICLE_0 =
40         "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
41         "<tr>" +
42         "<td width=\"220\"><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
43         "<tr>" +
44         "<td class=\"mblau\" width=\"22\"><A HREF=\"<TK_APP_CONTEXT>/servlet/WebManServlet\" target=\"_top\"" +
45         " onmouseover=\"omChangeImage('start', '<TK_APP_CONTEXT>/templates/images/go_start_on.gif')\"" +
46         " onmouseout=\"omChangeImage('start', '<TK_APP_CONTEXT>/templates/images/go_start_off.gif')\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/go_start_off.gif\" width=\"22\" height=\"42\" border=\"0\" alt=\"\" name=\"start\"></a></td>" +
47         "<td width=\"1\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/line.gif\" width=\"1\" height=\"42\" border=\"0\" alt=\"\"></td>" +
48         "<td class=\"btitel\" width=\"196\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/i_generator.gif\" width=\"38\" height=\"34\" border=\"0\" alt=\"\">";
49
50     public static String JavaDoc DOCUMENT_PARTICLE_1 =
51         "<br>" +
52         "<img SRC=\"<TK_APP_CONTEXT>/templates/images/mblau.gif\" width=\"195\" height=\"1\" border=\"0\" alt=\"\"></td>" +
53         "<td width=\"1\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/line.gif\" width=\"1\" height=\"42\" border=\"0\" alt=\"\"></td>" +
54         "</tr>" +
55         "</table></td>" +
56         "<td class=\"weiss\" valign=\"top\" width=\"275\"><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
57         "<tr>" +
58         "<td class=\"dgrau\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/dgrau.gif\" width=\"19\" height=\"18\" border=\"0\" alt=\"\"></td>" +
59         "<td class=\"dgrau\">angemeldet als:</td>" +
60         "<td class=\"weiss\">&nbsp;</td>" +
61         "</tr>" +
62         "<tr>" +
63         "<td class=\"l\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/line.gif\" width=\"19\" height=\"1\" border=\"0\" alt=\"\"></td>" +
64         "<td class=\"l\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/line.gif\" width=\"210\" height=\"1\" border=\"0\" alt=\"\"></td>" +
65         "<td class=\"l\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/line.gif\" width=\"46\" height=\"1\" border=\"0\" alt=\"\"></td>" +
66         "</tr>" +
67         "<tr>" +
68         "<td class=\"hgrau\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/hgrau.gif\" width=\"19\" height=\"23\" border=\"0\" alt=\"\"></td>" +
69         "<td class=\"hgrau\">";
70
71     public static String JavaDoc DOCUMENT_PARTICLE_2 =
72         "</td>" +
73         "<td class=\"weiss\">&nbsp;</td>" +
74         "</tr>" +
75         "</table></td>" +
76         "<td class=\"weiss\" align=\"right\" valign=\"top\" width=\"100%\"><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
77         "<tr><td width=\"100%\">&nbsp;</td>" +
78         "<td><a HREF=\"http://www.webman.de\" target=\"_blank\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/logo.gif\" width=\"165\" height=\"42\"" +
79         "border=\"0\" alt=\" At your side. For your site. \"></a></td>" +
80         "<td valign=\"bottom\" class=\"version\">" +
81         getVersion() +
82         "<br><img SRC=\"<TK_APP_CONTEXT>/templates/images/weiss.gif\" width=\"10\" height=\"3\" border=\"0\" alt=\"\"></td>" +
83         "<td><img SRC=\"<TK_APP_CONTEXT>/templates/images/weiss.gif\" width=\"23\" height=\"1\" border=\"0\" alt=\"\"></td>" +
84         "</tr></table></td>" +
85         "</tr>" +
86         "</table>" + LINE_SEPARATOR +
87         "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
88         "<tr>" +
89         "<td class=\"l\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/line.gif\" width=\"22\" height=\"1\" border=\"0\" alt=\"\"></td>" +
90         "<td class=\"l\" width=\"1\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/line.gif\" width=\"1\" height=\"1\" border=\"0\" alt=\"\"></td>" +
91         "<td class=\"l\" width=\"100%\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/line.gif\" width=\"1\" height=\"1\" border=\"0\" alt=\"\"></td>" +
92         "</tr>" +
93         "<tr>" +
94         "<td class=\"dblau\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/dblau.gif\" width=\"22\" height=\"6\" border=\"0\" alt=\"\"></td>" +
95         "<td class=\"mblau\" width=\"1\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/line.gif\" width=\"1\" height=\"6\" border=\"0\" alt=\"\"></td>" +
96         "<td class=\"mblau\" width=\"100%\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/mblau.gif\" width=\"400\" height=\"6\" border=\"0\" alt=\"\"></td>" +
97         "</tr>" +
98         "<tr>" +
99         "<td class=\"l\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/line.gif\" width=\"1\" height=\"1\" border=\"0\" alt=\"\"></td>" +
100         "<td class=\"l\" width=\"1\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/line.gif\" width=\"1\" height=\"1\" border=\"0\" alt=\"\"></td>" +
101         "<td class=\"l\" width=\"100%\"\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/line.gif\" width=\"400\" height=\"1\" border=\"0\" alt=\"\"></td>" +
102         "</tr>" +
103         "</table>";
104
105     public static String JavaDoc TABLE_START =
106         "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
107         "<tr>" +
108         "<td class=\"mblau\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/mblau.gif\" width=\"22\" height=\"1\" border=\"0\" alt=\"\"></td>" +
109         "<td class=\"l\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/line.gif\" width=\"1\" height=\"1\" border=\"0\" alt=\"\"></td>" +
110         "<td class=\"mblau\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/mblau.gif\" width=\"78\" height=\"6\" border=\"0\" alt=\"\"></td>";
111
112     public static String JavaDoc TABLE_END = "</tr></table>";
113
114     public static String JavaDoc BODY_END = "</BODY></HTML>";
115
116     public static String JavaDoc LINE_STANDARD = "<td class=\"start\">";
117
118     public static String JavaDoc LINE_SUCCESS = "<td class=\"number\">";
119
120     public static String JavaDoc LINE_FAILURE = "<td class=\"warning\">";
121
122     public static String JavaDoc PARAGRAPH_EMPTY =
123         "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
124         "<tr>" +
125         "<td class=\"mblau\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/mblau.gif\" width=\"22\" height=\"1\" border=\"0\" alt=\"\"></td>" +
126         "<td class=\"l\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/line.gif\" width=\"1\" height=\"1\" border=\"0\" alt=\"\"></td>" +
127         "<td class=\"mblau\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/mblau.gif\" width=\"78\" height=\"6\" border=\"0\" alt=\"\"></td>" +
128         "<td class=\"mblau\">" +
129         "<p>&nbsp;</p>" +
130         "</tr>" +
131         "</table>";
132
133     public static String JavaDoc PARAGRAPH_LINE =
134         "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
135         "<tr>" +
136         "<td class=\"l\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/line.gif\" width=\"22\" height=\"1\" border=\"0\" alt=\"\"></td>" +
137         "<td class=\"l\" width=\"1\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/line.gif\" width=\"1\" height=\"1\" border=\"0\" alt=\"\"></td>" +
138         "<td class=\"l\" width=\"100%\"><img SRC=\"<TK_APP_CONTEXT>/templates/images/line.gif\" width=\"1\" height=\"1\" border=\"0\" alt=\"\"></td>" +
139         "</tr>" +
140         "</table>";
141
142     private static String JavaDoc TK_APP_CONTEXT = "<TK_APP_CONTEXT>";
143
144
145     // Attributes
146

147     /**
148      * Indicates wether a line is already opened.
149      */

150     private boolean open = false;
151
152     /**
153      * The original writer.
154      */

155     private PrintWriter writer = null;
156
157     /**
158      * The context to be prepended to all URLs in HTML output.
159      */

160     private String JavaDoc context = null;
161
162
163     // Constructor
164

165     /**
166      * Creates a HTML wrapper for generator event handlers.
167      *
168      * @param writer the output stream.
169      * @param context the context to be prepended to all URLs
170      * in HTML output.
171      */

172     public GRHTMLWrapper (PrintWriter writer, String JavaDoc context)
173     {
174         super(writer);
175
176         this.writer = writer;
177         this.context = context;
178     }
179
180     private static String JavaDoc getVersion()
181     {
182         try
183         {
184             return LanguageManager.getText("gui", "WEBMAN_VER");
185         }
186         catch (Exception JavaDoc e)
187         {
188             return "unknown";
189         }
190     }
191
192     // Overridden methods
193

194     /**
195      * Closes the stream.
196      */

197     public synchronized void close ()
198     {
199         writer.println(substitute(BODY_END));
200         writer.close();
201     }
202
203     /**
204      * Write a single character.
205      */

206     public synchronized void write (int c)
207     {
208         linePrefix();
209         writer.write(c);
210     }
211
212     /**
213      * Write a portion of an array of characters.
214      */

215     public synchronized void write (char[] buf, int off, int len)
216     {
217         linePrefix();
218         writer.write(buf, off, len);
219     }
220
221     /**
222      * Write an array of characters.
223      */

224     public synchronized void write (char[] buf)
225     {
226         linePrefix();
227         writer.write(buf);
228     }
229
230     /**
231      * Write a portion of a string.
232      */

233     public synchronized void write (String JavaDoc s, int off, int len)
234     {
235         linePrefix();
236         writer.write(s, off, len);
237     }
238
239     /**
240      * Write a string.
241      */

242     public synchronized void write (String JavaDoc s)
243     {
244         linePrefix();
245         writer.write(s);
246     }
247
248
249     // Convenience
250

251     /**
252      * Prints the HTML document header to the stream.
253      *
254      * @param name the name of the calling app, i.e. generator or publisher.
255      * @param login the name of the current user.
256      */

257     public synchronized void start (String JavaDoc name, String JavaDoc login)
258     {
259         writer.println(substitute(HEADER_START));
260         writer.println(substitute(HEADER_META));
261         writer.println(substitute(HEADER_SCRIPT));
262         writer.println(substitute(HEADER_STYLE));
263         writer.print(substitute(HEADER_TITLE_START));
264         writer.print(name);
265         writer.println(substitute(HEADER_TITLE_END));
266         writer.println(substitute(HEADER_END));
267
268         writer.println(substitute(BODY_START));
269         writer.print(substitute(DOCUMENT_PARTICLE_0));
270         writer.print(name);
271         writer.print(substitute(DOCUMENT_PARTICLE_1));
272         writer.print(login);
273         writer.println(substitute(DOCUMENT_PARTICLE_2));
274
275         writer.println(substitute(PARAGRAPH_EMPTY));
276     }
277
278     /**
279      * Prints the HTML document footer to the stream.
280      *
281      * @param success indicates wether the app using the stream
282      * was finished successful.
283      */

284     public synchronized void end (boolean success)
285     {
286         lineSuffix();
287
288         writer.println(substitute(PARAGRAPH_EMPTY));
289         writer.println(substitute(PARAGRAPH_LINE));
290         writer.println(substitute(PARAGRAPH_EMPTY));
291
292         if (! open)
293         {
294             writer.print(substitute(TABLE_START));
295             open = true;
296         }
297
298         writer.print(substitute(success ? LINE_SUCCESS : LINE_FAILURE));
299         writer.print(success ? "Successfully finished" : "Finished with errors");
300
301         lineSuffix();
302
303         writer.println(substitute(PARAGRAPH_EMPTY));
304         writer.println(substitute(PARAGRAPH_LINE));
305         writer.println(substitute(PARAGRAPH_EMPTY));
306     }
307
308     public void println(String JavaDoc line)
309     {
310         linePrefix();
311         writer.print("<font color=" + getColor(line) + ">");
312         writer.println(line);
313         writer.print("</font>");
314         lineSuffix();
315
316     }
317
318     /**
319      * Terminate the current line by writing the line separator string.
320      */

321     public synchronized void println ()
322     {
323         linePrefix();
324         lineSuffix();
325     }
326
327     /**
328      * wie println, nur wird die Zeile noch nicht abgeschlossen
329      */

330     public void print(String JavaDoc line)
331     {
332         linePrefix();
333         writer.print("<font color=" + getColor(line) + ">");
334         writer.println(line);
335         writer.print("</font>");
336     }
337
338     /**
339      * liefert die Farbe zurueck, in der diese Zeile ausgegeben
340      * werden soll anhand der Log4J Prioritaeten
341      */

342     private String JavaDoc getColor(String JavaDoc in)
343     {
344         if (in.indexOf("INFO")!= -1)
345             return "white";
346         if (in.indexOf("WARN") != -1)
347             return "yellow";
348         if (in.indexOf("ERROR") != -1 || in.indexOf("FATAL") != -1)
349             return "#ff5555"; //dieses rot harmoniert besser mit dem blauen hintergrund
350
if (in.toLowerCase().indexOf(" done") != -1
351             || in.toLowerCase().indexOf(" success") != -1
352             || in.toLowerCase().indexOf(" succeeded") != -1)
353             return "#00cc00"; //dieses grün harmoniert besser mit dem blauen hintergrund
354
return "black";
355     }
356
357     // Styled HTML
358

359     private void linePrefix ()
360     {
361         if (! open)
362         {
363             writer.print(substitute(TABLE_START));
364             writer.print(substitute(LINE_STANDARD));
365             open = true;
366         }
367     }
368
369     private void lineSuffix ()
370     {
371         if (open)
372         {
373             writer.println(substitute(TABLE_END));
374             open = false;
375         }
376     }
377
378
379     // Rudimentary tag substitution
380

381     private String JavaDoc substitute (String JavaDoc string)
382     {
383         int index = string.indexOf(TK_APP_CONTEXT);
384
385         if (index == -1)
386         {
387             return string;
388         }
389
390         int count = 0;
391         int diff = this.context.length() - TK_APP_CONTEXT.length();
392         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(string);
393
394         while (index != -1)
395         {
396             buffer.replace(index + (count * diff), index + (count * diff) + TK_APP_CONTEXT.length(), this.context);
397             index = string.indexOf(TK_APP_CONTEXT, index + 1);
398             count++;
399         }
400
401         return buffer.toString();
402     }
403
404 }
405
Popular Tags