KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > util > HtmlUtilities


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

45 package org.openejb.util;
46
47 /**
48  * A class for HTML utilities; see
49  * <a HREF="http://www.w3.org/TR/html4" target="_blank">http://www.w3.org/TR/html4</a>
50  * for more info
51  *
52  * @author <a HREF="mailto:tim_urberg@yahoo.com">Tim Urberg</a>
53  */

54 public class HtmlUtilities {
55     /** type for &lt;a name="name"...&gt; */
56     public static final String JavaDoc ANCHOR_NAME_TYPE = "name";
57     /** type for &lt;a HREF="href"...&gt; */
58     public static final String JavaDoc ANCHOR_HREF_TYPE = "href";
59
60     //we don't want anyone creating new instances of this class
61
private HtmlUtilities() {}
62
63     /**
64      * Creates an HTML anchor. If <code>ANCHOR_NAME_TYPE</code> is passed in,
65      * it creates a name anchor:<br><br>
66      * <code>
67      * &lt;a name="value"&gt;display&lt;/a&gt;
68      * </code><br><br>
69      * If <code>ANCHOR_HREF_TYPE</code> is passed in it creates an href anchor:
70      * <br><br>
71      * <code>
72      * &lt;a HREF="href"&gt;display&lt;/a&gt;
73      * </code>
74      *
75      * @see org.openejb.util.HtmlUtilities#ANCHOR_NAME_TYPE
76      * @see org.openejb.util.HtmlUtilities#ANCHOR_HREF_TYPE
77      * @param value the name or href value for this anchor
78      * @param display the display for this anchor
79      * @param type the type of this anchor (name or href)
80      * @return an HTML anchor element
81      */

82     public static String JavaDoc createAnchor(String JavaDoc value, String JavaDoc display, String JavaDoc type) {
83         //our type must be one of these two
84
if (!(ANCHOR_HREF_TYPE.equals(type) || ANCHOR_NAME_TYPE.equals(type))) {
85             throw new IllegalArgumentException JavaDoc("The type argument must be either \"name\" or \"href\"");
86         }
87
88         return new StringBuffer JavaDoc(100)
89             .append("<a ")
90             .append(type)
91             .append("=\"")
92             .append(value)
93             .append("\">")
94             .append(display)
95             .append("</a>")
96             .toString();
97     }
98
99     /**
100      * Creates the beginning of an HTML select based on the name passed in
101      * <br>
102      * <code>
103      * &lt;select name="name" onChange="onChange"&gt;
104      * </code>
105      * <br>
106      * @param name the name of the select form field
107      * @param onChange a JavaScript onChange event (pass in null for no onChange)
108      * @return the constructed select, similar to above
109      */

110     public static String JavaDoc createSelectFormField(String JavaDoc name, String JavaDoc onChange) {
111         StringBuffer JavaDoc temp = new StringBuffer JavaDoc(60).append("<select name=\"").append(name);
112
113         if (onChange != null) {
114             temp.append("\" onChange=\"").append(onChange);
115         }
116
117         return temp.append("\">").toString();
118     }
119
120     /**
121      * Creates an HTML option used inside an HTML select
122      * <br>
123      * <code>
124      * &lt;option value="value" selected&gt;display&lt;/option&gt;
125      * </code>
126      * <br>
127      * @param value the value for this option
128      * @param display the display for this option
129      * @param selected whether or not this option should be selected
130      * @return the constructed option, similar to above
131      */

132     public static String JavaDoc createSelectOption(String JavaDoc value, String JavaDoc display, boolean selected) {
133         StringBuffer JavaDoc temp = new StringBuffer JavaDoc(65).append("<option value=\"").append(value).append("\"");
134
135         if (selected) {
136             temp.append(" selected");
137         }
138
139         return temp.append(">").append(display).append("</option>").toString();
140     }
141
142     /**
143      * Creates an HTML text form field based on the parameters passed in
144      * <br>
145      * <code>
146      * &lt;input type="text" name="name" value="value" size="size" maxlength="maxlength"&gt;
147      * </code>
148      * <br>
149      * @param name the name of the text form field
150      * @param value the value of the text form field
151      * @param size the size of the text form field (0 for no size)
152      * @param maxLength the maxlength of the text form field (0 for no maxlength)
153      * @return the constructed text form field, similar to above
154      */

155     public static String JavaDoc createTextFormField(String JavaDoc name, String JavaDoc value, int size, int maxLength) {
156         return createInputFormField("text", name, value, size, maxLength, null, null, null, null, false, false, false);
157     }
158     
159     /**
160      * Creates an HTML file form field based on the parameters passed in
161      * <br>
162      * <code>
163      * &lt;input type="file" name="name" value="value" size="size"&gt;
164      * </code>
165      * <br>
166      * @param name the name of the file form field
167      * @param value the value of the file form field
168      * @param size the size of the file form field (0 for no size)
169      * @return the constructed file form field, similar to above
170      */

171     public static String JavaDoc createFileFormField(String JavaDoc name, String JavaDoc value, int size) {
172         return createInputFormField("file", name, value, size, 0, null, null, null, null, false, false, false);
173     }
174
175     /**
176      * Creates an HTML hidden form field based on the parameters passed in
177      * <br>
178      * <code>
179      * &lt;input type="hidden" name="name" value="value"&gt;
180      * </code>
181      * <br>
182      * @param name the name of hidden form field
183      * @param value the value of the hidden form field
184      * @return the constructed hidden form field, similar to above
185      */

186     public static String JavaDoc createHiddenFormField(String JavaDoc name, String JavaDoc value) {
187         return createInputFormField("hidden", name, value, 0, 0, null, null, null, null, false, false, false);
188     }
189
190     /**
191      * Creates an HTML submit button based on the parameters passed in
192      * <br>
193      * <code>
194      * &lt;input type="submit" name="name" value="value"&gt;
195      * </code>
196      * <br>
197      * @param name the name of hidden form field
198      * @param value the value of the hidden form field
199      * @return the constructed hidden form field, similar to above
200      */

201     public static String JavaDoc createSubmitFormButton(String JavaDoc name, String JavaDoc value) {
202         return createInputFormField("submit", name, value, 0, 0, null, null, null, null, false, false, false);
203     }
204
205     /** creates an input type, text, radio, button submit, etc
206      *
207      * @param type the type of input
208      * @param name the name of the input
209      * @param value the value of the input
210      * @param size the size of the input (0 for no size)
211      * @param maxLength the maxlength of the input (0 for no maxlength
212      * @param onFocus an onfocus event (null for no onfocus)
213      * @param onBlur an onblur event (null for no onblur)
214      * @param onChange an onchange event (null for no onchange)
215      * @param onClick an onclick event (null for no onclick)
216      * @param checked if this input is checked
217      * @param disabled if this input is disabled
218      * @param readOnly if this input is readonly
219      * @return the constructed input
220      */

221     public static String JavaDoc createInputFormField(
222         String JavaDoc type,
223         String JavaDoc name,
224         String JavaDoc value,
225         int size,
226         int maxLength,
227         String JavaDoc onFocus,
228         String JavaDoc onBlur,
229         String JavaDoc onChange,
230         String JavaDoc onClick,
231         boolean checked,
232         boolean disabled,
233         boolean readOnly) {
234
235         StringBuffer JavaDoc temp =
236             new StringBuffer JavaDoc(150)
237                 .append("<input type=\"")
238                 .append(type)
239                 .append("\" name=\"")
240                 .append(name)
241                 .append("\" value=\"")
242                 .append(value)
243                 .append("\"");
244
245         if (size > 0) {
246             temp.append(" size=\"").append(size).append("\"");
247         }
248         if (maxLength > 0) {
249             temp.append(" maxlength=\"").append(maxLength).append("\"");
250         }
251         if (onFocus != null) {
252             temp.append(" onfocus=\"").append(onFocus).append("\"");
253         }
254         if (onBlur != null) {
255             temp.append(" onblur=\"").append(onBlur).append("\"");
256         }
257         if (onChange != null) {
258             temp.append(" onchange=\"").append(onChange).append("\"");
259         }
260         if (onClick != null) {
261             temp.append(" onclick=\"").append(onClick).append("\"");
262         }
263         if (checked) {
264             temp.append(" checked");
265         }
266         if (disabled) {
267             temp.append(" disabled");
268         }
269         if (readOnly) {
270             temp.append(" readonly");
271         }
272
273         return temp.append(">").toString();
274     }
275
276     /**
277      * Creates an HTML textarea object<br>
278      * <code>
279      * &lt;textarea name="name" rows="rows" cols="columns" onfocus="onFocus" onblur="onBlur" onchange="onChange"&gt;<br>
280      * content<br>
281      * &lt;/textarea&gt;
282      * </code>
283      *
284      * @param name the name of the textarea
285      * @param content the content of the textarea
286      * @param rows the number of rows of the textarea
287      * @param columns the numbe of columns of the textarea
288      * @param onFocus a javascript onfocus event (null for no onfocus)
289      * @param onBlur a javascript onblur event (null for no onblur)
290      * @param onChange a javascript onchange event (null for no onchange)
291      * @return the constucted textarea string
292      */

293     public static String JavaDoc createTextArea(
294         String JavaDoc name,
295         String JavaDoc content,
296         int rows,
297         int columns,
298         String JavaDoc onFocus,
299         String JavaDoc onBlur,
300         String JavaDoc onChange) {
301         StringBuffer JavaDoc temp = new StringBuffer JavaDoc(50);
302         temp.append("<textarea name=\"").append(name).append("\" rows=\"").append(rows).append("\" cols=\"").append(
303             columns).append(
304             "\"");
305
306         if (onFocus != null) {
307             temp.append(" onfocus=\"").append(onFocus).append("\"");
308         }
309         if (onBlur != null) {
310             temp.append(" onblur=\"").append(onBlur).append("\"");
311         }
312         if (onChange != null) {
313             temp.append(" onchange=\"").append(onChange).append("\"");
314         }
315
316         return temp.append(">").append(content).append("</textarea>").toString();
317     }
318 }
319
Popular Tags