Source code: com/voytechs/html/io/HtmlWriter.java
1 /*
2 * File: HtmlWriter.java
3 * Auth: Mark Bednarczyk
4 * Date: DATE
5 * Id: $Id: HtmlWriter.java,v 1.1.1.1 2002/01/23 23:52:45 voytechs Exp $
6 ********************************************
7 * $Log: HtmlWriter.java,v $
8 * Revision 1.1.1.1 2002/01/23 23:52:45 voytechs
9 * Initial public release, BETA 1.0 - voytechs
10 *
11 */
12 package com.voytechs.html.io;
13
14 import java.lang.*;
15 import java.util.*;
16
17 import java.io.*;
18
19 /**
20 * Html Print Writer.
21 * Will output HTML primitives on the supplied outputstream.
22 * Because Http protocol allows binary output to the stream as long
23 * as the appropriate MIME header is supplied, this object also allows
24 * access to the underlying output stream so binary data can be output
25 * as well if needed (getOutputStream());
26 * <BR>
27 * This writer supplies all of the basic HTML 4.0 tags. It also supplies
28 * some convenience functions.
29 * <BR>
30 * 3 types of HTML tag methods exist.
31 * <OL>
32 * <LI> *Start() and *End() mentods. ie. formStart() & formEnd()
33 * <LI> Convenience functions that supply both start and end tags. ie form(String formBody)
34 * <LI> Tags that do not have closing Tags. ie horizontalRule()
35 * </OL>
36 */
37 public class HtmlWriter
38 extends PrintWriter {
39
40 /* Internal attributes */
41 private OutputStream outStream = null;
42
43 /**
44 *
45 * @param outStream Output stream on to which write the text HTML primitives.
46 * @exception IOException any IO errors from output stream.
47 */
48 public HtmlWriter(OutputStream outStream) throws IOException {
49 super(outStream);
50
51 this.outStream = outStream;
52 }
53
54 /**
55 * Get the underlying output stream of this writer.
56 * @return RAW output stream.
57 */
58 public OutputStream getOutputStream() {
59 return(outStream);
60 }
61
62
63
64
65 //////////////////////////// START OF HTML TAG METHODS ////////////////////////
66
67 /**
68 * HTML document START tag.
69 */
70 public void html() throws IOException {
71 println("<HTML>");
72 }
73
74 /**
75 * HTML document END tag.
76 */
77 public void htmlEnd() throws IOException {
78 println("</HTML>");
79 }
80
81 /**
82 * HTML body START tag. Requires an END tag.
83 */
84 public void body() throws IOException {
85 println("<BODY>");
86 }
87
88 /**
89 * HTML body START tag. Requires an END tag.
90 * @param bgColor Background color of the document.
91 * @param fgColor Foreground color of the document.
92 */
93 public void body(String params) throws IOException {
94 println("<BODY " + params + ">");
95 }
96
97 /**
98 * HTML body END tag.
99 */
100 public void bodyEnd() throws IOException {
101 println("</BODY>");
102 }
103
104 /**
105 * HTML document header START tag. Requires an END tag.
106 */
107 public void head() throws IOException {
108 println("<HEAD>");
109 }
110
111 /**
112 * HTML document header START tag. Requires an END tag.
113 */
114 public void head(String params) throws IOException {
115 println("<HEAD " + params + ">");
116 }
117
118 /**
119 * HTML document header END tag.
120 */
121 public void headEnd() throws IOException {
122 println("</HEAD>");
123 }
124
125 /**
126 * HTML anchor START tag. Requires an END tag.
127 */
128 public void a(String href, String params) throws IOException {
129 println("<A HREF='" + href + "' " + params + ">");
130 }
131
132 /**
133 * HTML anchor END tag.
134 */
135 public void aEnd() throws IOException {
136 println("</A>");
137 }
138
139 /**
140 * HTML FORM START tag. The default HTTP POST method is used. Requires an END tag.
141 * @param action URL to send the form to when the submit button is pressed.
142 */
143 public void form(String actionUrl, String params) throws IOException {
144 println("<FORM ACTION='" + actionUrl + "' " + params + ">");
145 }
146
147 /**
148 * HTML FORM END tag.
149 */
150 public void formEnd() throws IOException {
151 println("</FORM>");
152 }
153
154 /**
155 * HTML INPUT tag.
156 * @param name Name for this form element.
157 * @param value Value of this form element.
158 */
159 public void input(String type, String name, String params) throws IOException {
160 println("<INPUT TYPE=" + type + " NAME='" + name + "' " + params + ">");
161 }
162
163 /**
164 * HTML INPUT TYPE=TEXT tag.
165 * @param name Name for this form element.
166 * @param value Value of this form element.
167 */
168 public void text(String name, String params) throws IOException {
169 println("<INPUT TYPE=TEXT NAME='" + name + "' " + params + ">");
170 }
171
172 /**
173 * HTML INPUT TYPE=PASSWORD tag.
174 * @param name Name for this form element.
175 * @param value Value of this form element.
176 */
177 public void password(String name, String params) throws IOException {
178 println("<INPUT TYPE=PASSWORD NAME='" + name + "' " + params + ">");
179 }
180
181 /**
182 * HTML INPUT TYPE=SUBMIT tag.
183 * @param name Name for this form element.
184 * @param value Value of this form element.
185 */
186 public void submit(String name, String params) throws IOException {
187 println("<INPUT TYPE=SUBMIT NAME='" + name + "' " + params + ">");
188 }
189
190 /**
191 * HTML INPUT TYPE=RADIO tag.
192 * @param name Name for this form element.
193 * @param index Index value/string of the entry.
194 * @param label Label of this radio element.
195 */
196 public void radio(String name, String index, String label, String params) throws IOException {
197 println("<INPUT TYPE=RADIO NAME='" + name + "' VALUE='" + index + "' " + params + ">" + label);
198 }
199
200 /**
201 * HTML INPUT TYPE=CHECKBOX tag.
202 * @param name Name for this form element.
203 * @param value Value of this form element.
204 */
205 public void checkbox(String name, String index, String label, String params) throws IOException {
206 println("<INPUT TYPE=CHECKBOX NAME='" + name + "' VALUE='" + index + "' " + params + ">" + label);
207 }
208
209 /**
210 * HTML INPUT TYPE=RESET tag.
211 * @param name Name for this form element.
212 */
213 public void reset(String params) throws IOException {
214 println("<INPUT TYPE=RESET " + params + ">");
215 }
216
217 /**
218 * HTML Textarea START tag. Requires an END tag.
219 * @param name Name for this form element.
220 */
221 public void textarea(String name, String params) throws IOException {
222 println("<TEXTAREA NAME='" + "' " + params + ">");
223 }
224
225 /**
226 * HTML Textarea END tag.
227 */
228 public void textareaEnd() throws IOException {
229 println("</TEXTAREA>");
230 }
231
232 /**
233 * HTML SELECT START tag. Selection list box. Requires an END tag.
234 * @param name Name for this form element.
235 */
236 public void select(String name, String params) throws IOException {
237 println("<SELECT NAME='" + name + "' " + params + ">");
238 }
239
240 /**
241 * HTML SELECT END tag. Selection list box.
242 */
243 public void selectEnd() throws IOException {
244 println("</SELECT>");
245 }
246
247 /**
248 * HTML OPTION tag. List box element.
249 * @param label Label displayed for this list box element.
250 * @param param Any additional tag parameters in HTML TAG format.
251 */
252 public void option(String index, String label, String params) throws IOException {
253 println("<OPTION VALUE='" + index + "' " + params + ">" + label);
254 }
255
256 /**
257 * HTML TABLE START tag. Requires an END tag.
258 * @param border Width of the border for this table.
259 */
260 public void table(String params) throws IOException {
261 println("<TABLE " + params + ">");
262 }
263
264 /**
265 * HTML TABLE END tag.
266 */
267 public void tableEnd() throws IOException {
268 println("</TABLE>");
269 }
270
271 public void tBody(String params) throws IOException { println("<TBODY " + params + ">"); }
272 public void tBodyEnd() throws IOException { println("</TBODY>"); }
273
274 public void tHead(String params) throws IOException { println("<THEAD " + params + ">"); }
275 public void tHeadEnd() throws IOException { println("</THEAD>"); }
276
277 public void tFoot(String params) throws IOException { println("<TFOOT " + params + ">"); }
278 public void tFootEnd() throws IOException { println("</TFOOT>"); }
279
280 public void caption(String params) throws IOException { print("<CAPTION " + params + ">"); }
281 public void captionEnd() throws IOException { println("</CAPTION>"); }
282
283
284 /**
285 * HTML TABLE HEADER START tag. Requires an END tag.
286 */
287 public void th(String params) throws IOException {
288 println("<TH " + params + ">");
289 }
290
291 /**
292 * HTML TABLE HEADER END tag. Requires an END tag.
293 */
294 public void thEnd() throws IOException {
295 println("</TH>");
296 }
297
298 /**
299 * HTML TABLE ROW START tag. Requires an END tag.
300 * @param border Width of the border for this table.
301 */
302 public void tr(String params) throws IOException {
303 println("<TR " + params + ">");
304 }
305
306 /**
307 * HTML TABLE ROW END tag. Requires an END tag.
308 * @param border Width of the border for this table.
309 */
310 public void trEnd() throws IOException {
311 println("</TR>");
312 }
313
314 /**
315 * HTML TABLE DATA START tag. Requires an END tag.
316 * @param border Width of the border for this table.
317 */
318 public void td(String params) throws IOException {
319 print("<TD " + params + ">");
320 }
321
322 /**
323 * HTML TABLE DATA END tag. Requires an END tag.
324 * @param border Width of the border for this table.
325 */
326 public void tdEnd() throws IOException {
327 println("</TD>");
328 }
329
330 /**
331 * HTML SCRIPT START tag. Requires an END tag.
332 * @param border Width of the border for this table.
333 */
334 public void script(String language, String params) throws IOException {
335 println("<SCRIPT LANGUAGE='" + language + "' " + params + ">");
336 }
337
338 /**
339 * HTML SCRIPT END tag. Requires an END tag.
340 * @param border Width of the border for this table.
341 */
342 public void scriptEnd() throws IOException {
343 println("</SCRIPT>");
344 }
345
346 /**
347 * HTML SCRIPT END tag. Requires an END tag.
348 * @param border Width of the border for this table.
349 */
350 public void img(String uri, String params) throws IOException {
351 println("<IMG SRC='" + uri + "' " + params + ">");
352 }
353
354 public void br() throws IOException { println("<BR>"); }
355 public void hr() throws IOException { println("<HR>"); }
356 public void i() throws IOException { println("<I>"); }
357 public void iEnd() throws IOException { println("</I>"); }
358 public void b() throws IOException { println("<B>"); }
359 public void bEnd() throws IOException { println("</B>"); }
360
361 /**
362 * Test function for HtmlWriter
363 * @param args command line arguments
364 */
365 public static void main(String [] args) {
366 }
367
368 } /* END OF: HtmlWriter */