KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > view > HtmlViewToolkit


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.view;
8
9
10 import java.io.UnsupportedEncodingException JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import com.inversoft.util.ObjectTools;
16 import com.inversoft.util.StringTools;
17
18
19 /**
20  * This class is the toolkit that developers can use it order
21  * to help create new views that work within the framework.
22  * The framework is designed so that a specific language or
23  * specification must be followed for the request parameters
24  * in order for the ActionServlet to correctly handle and
25  * delegate requests. This language is described in detail
26  * in the documentation. Most of the necessary logic for building
27  * parameter names and values is stored in this toolkit. This
28  * toolkit also contains various methods used by the JSP view.
29  *
30  * @author Brian Pontarelli
31  */

32 public class HtmlViewToolkit {
33
34     /* Used to help create unique names */
35     private volatile static int unique = 0;
36
37
38     /** Private constructor */
39     private HtmlViewToolkit() {
40         // You can't make me
41
}
42
43
44     //-------------------------------------------------------------------------
45
// Helper hethods
46
//-------------------------------------------------------------------------
47

48
49     /**
50      * Returns a unique name that can be used for input tags or other entities.
51      * This is guarenteed to be unique across VMs. This could be a bottle neck,
52      * so I am removing the synchronization. This should be okay because a JSP
53      * will be tied to a session and the compiled code is executed linearly,
54      * which means for a single page, this should still remain unique.
55      */

56     public static String JavaDoc createUniqueName() {
57         return HtmlConstants.INPUT_NAME_PREFIX + unique++;
58     }
59
60
61     /**
62      * Returns the current value of a text input tag. This uses the object and
63      * the string to determine whether or not the input tags value should be
64      * the object (to string) or the string. This is useful when determining
65      * whether or not the input tag should have the value of the bean property
66      * it is associated with or the default value set on the HTML / JSP page.
67      * This method will return the string value if it is not null. If it is
68      * null it will return the object value if it is not null. If that is null
69      * also, it will return an empty string (not null).
70      *
71      * @param value The possible string value for the text input
72      * @param currentValue The possible object value for the text input
73      * @return The string value, described above.
74      */

75     public static String JavaDoc makeValueString(Object JavaDoc value, Object JavaDoc currentValue) {
76         String JavaDoc str = null;
77         if (value == null) {
78             str = ObjectTools.cleanToString(currentValue);
79         } else {
80             str = ObjectTools.cleanToString(value);
81         }
82
83         return str;
84     }
85
86     /**
87      * Constructa a hidden input tag using the given name and value and appends
88      * it to the given string buffer.
89      *
90      * @param buf The string buffer to append to
91      * @param name The name of the hidden tag
92      * @param value The value of the hidden tag
93      */

94     public static void appendHiddenTag(StringBuffer JavaDoc buf, String JavaDoc name, String JavaDoc value) {
95         appendHiddenTag(buf, null, name, value);
96     }
97
98     /**
99      * Constructa a hidden input tag using the given name and value and appends
100      * it to the given string buffer.
101      *
102      * @param buf The string buffer to append to
103      * @param id The ID of the hidden tag
104      * @param name The name of the hidden tag
105      * @param value The value of the hidden tag
106      */

107     public static void appendHiddenTag(StringBuffer JavaDoc buf, String JavaDoc id, String JavaDoc name,
108             String JavaDoc value) {
109         if (value == null) {
110             value = "";
111         }
112
113         buf.append("<input type=\"hidden\"");
114
115         if (id != null) {
116             buf.append(" id=\"").append(id).append("\"");
117         }
118
119         buf.append(" name=\"").append(name);
120         buf.append("\" value=\"").append(value).append("\"/>");
121     }
122
123     /**
124      * Appends all the attributes to the buffer. The map contains attributes that
125      * have names and values and the list contains the attributes that don't have
126      * values.
127      *
128      * @param buf The string buffer to append the attributes to
129      * @param attributes (optional) The map that contains all the name-value
130      * attributes
131      * @param singleAttrs (optional) The list that contains the valueless
132      * attributes
133      */

134     public static void appendAttributes(StringBuffer JavaDoc buf, Map JavaDoc attributes,
135             List JavaDoc singleAttrs) {
136
137         if (attributes != null && !attributes.isEmpty()) {
138             Iterator JavaDoc iter = attributes.keySet().iterator();
139             Object JavaDoc key;
140             Object JavaDoc value;
141
142             while (iter.hasNext()) {
143                 key = iter.next();
144                 value = attributes.get(key);
145
146                 if (key != null && value != null) {
147                     buf.append(" ").append(key).append("=\"");
148                     buf.append(value).append("\"");
149                 }
150             }
151         }
152
153         if (singleAttrs != null && !singleAttrs.isEmpty()) {
154             Iterator JavaDoc iter = singleAttrs.iterator();
155             Object JavaDoc value;
156
157             while (iter.hasNext()) {
158                 value = iter.next();
159                 if (value != null) {
160                     buf.append(" ").append(value);
161                 }
162             }
163         }
164     }
165
166     /**
167      * A default level method that appends a single attribute to a given string
168      * buffer with the given name and value. If the attribute is null, it is not
169      * appended.
170      *
171      * @param buf The String buffer to append to
172      * @param name The name of the attribute
173      * @param value The value of the attribute
174      */

175     public static void appendAttribute(StringBuffer JavaDoc buf, String JavaDoc name, String JavaDoc value) {
176         if (value != null) {
177             buf.append(" ").append(name).append("=\"");
178             buf.append(value).append("\"");
179         }
180     }
181
182     /**
183      * Appends the given parameter in URL parameter form to the given string buffer.
184      *
185      * @param buf The String buffer to append to
186      * @param name The name of the attribute
187      * @param value The value of the attribute
188      */

189     public static void appendURLParameter(StringBuffer JavaDoc buf, String JavaDoc name, String JavaDoc value) {
190         if (StringTools.isTrimmedEmpty(name)) {
191             throw new NullPointerException JavaDoc("The name of a URL parameter can not " +
192                 "be null or empty");
193         }
194
195         try {
196             name = java.net.URLEncoder.encode(name, "UTF-8");
197
198             if (!StringTools.isTrimmedEmpty(value)) {
199                 value = java.net.URLEncoder.encode(value, "UTF-8");
200             } else {
201                 value = "";
202             }
203         } catch (UnsupportedEncodingException JavaDoc uee) {
204             assert (false) : uee.toString();
205         }
206
207
208         buf.append(name).append("=").append(value);
209     }
210
211
212     //-------------------------------------------------------------------------
213
// The input form tags
214
//-------------------------------------------------------------------------
215

216
217     /**
218      * <p>
219      * Appends the start of an input tag to the string buffer given. The tag
220      * type passed in is used when appending the input tag to the string buffer
221      * but is not checked for validity.
222      * </p>
223      *
224      * <p>
225      * The tag is not closed so that extra attributes can be appended to it as
226      * needed. The buffer is left with NO blank spaces on the end. If new
227      * attributes are to be added a space will also need to be added.
228      * </p>
229      *
230      * @param buf The string buffer to append to
231      * @param tagType The type of input tag to append
232      * @param name The name of the text input tag
233      * @param value (optional) The value of the input tag
234      */

235     public static void appendInputTagStart(StringBuffer JavaDoc buf, String JavaDoc tagType,
236             String JavaDoc id, String JavaDoc name, String JavaDoc value) {
237
238         buf.append("<input");
239         appendAttribute(buf, HtmlConstants.TYPE, tagType);
240         appendAttribute(buf, HtmlConstants.ID, id);
241         appendAttribute(buf, HtmlConstants.NAME, name);
242         appendAttribute(buf, HtmlConstants.VALUE, value);
243     }
244
245     /**
246      * Creates a text input tag that use the given name, value and attributes.
247      * The attributes are built from the Map and List given. The create tag is
248      * appended to the StringBuffer given.
249      *
250      * @param buf The StringBuffer to append to
251      * @param name The name of the text input tag
252      * @param value (optional) The value of the input tag
253      * @param attributes (optional) A hash of all the attributes with values
254      * to append after the name and value have been appended.
255      * @param singleAttrs (optional) A list of all the valueless attributes to
256      * append at the end of the tag
257      */

258     public static void createTextTag(StringBuffer JavaDoc buf, String JavaDoc id, String JavaDoc name,
259             String JavaDoc value, Map JavaDoc attributes, List JavaDoc singleAttrs) {
260
261         appendInputTagStart(buf, HtmlConstants.TEXT_TYPE, id, name, value);
262         appendAttributes(buf, attributes, singleAttrs);
263         buf.append("/>");
264     }
265
266     /**
267      * Creates a password input tag that use the given name, value and attributes.
268      * The attributes are built from the Map and List given. The create tag is
269      * appended to the StringBuffer given.
270      *
271      * @param buf The StringBuffer to append to
272      * @param name The name of the password input tag
273      * @param value (optional) The value of the input tag
274      * @param attributes (optional) A hash of all the attributes with values
275      * to append after the name and value have been appended.
276      * @param singleAttrs (optional) A list of all the valueless attributes to
277      * append at the end of the tag
278      */

279     public static void createPasswordTag(StringBuffer JavaDoc buf, String JavaDoc id, String JavaDoc name,
280             String JavaDoc value, Map JavaDoc attributes, List JavaDoc singleAttrs) {
281
282         appendInputTagStart(buf, HtmlConstants.PASSWORD_TYPE, id, name, value);
283         appendAttributes(buf, attributes, singleAttrs);
284         buf.append("/>");
285     }
286
287     /**
288      * <p>
289      * Creates a radio input tag that use the given name, value and attributes.
290      * The attributes are built from the Map and List given. The create tag is
291      * appended to the StringBuffer given. If the radio button is checked,
292      * the <code>checked</code> parameter should be true.
293      * </p>
294      *
295      * @param buf The StringBuffer to append to
296      * @param name The name of the radio tag
297      * @param value The value of the radio tag
298      * @param checked Determines whether or not the checked attribute is added
299      * tp the generated tag
300      * @param attributes (optional) A hash of all the attributes with values
301      * to append after the name and value have been appended.
302      * @param singleAttrs (optional) A list of all the valueless attributes to
303      * append at the end of the tag
304      */

305     public static void createRadioTag(StringBuffer JavaDoc buf, String JavaDoc id, String JavaDoc name,
306             String JavaDoc value, boolean checked, Map JavaDoc attributes, List JavaDoc singleAttrs) {
307
308         appendInputTagStart(buf, HtmlConstants.RADIO_TYPE, id, name, value);
309         if (checked) {
310             buf.append(" ").append(HtmlConstants.CHECKED);
311         }
312         appendAttributes(buf, attributes, singleAttrs);
313         buf.append("/>");
314     }
315
316     /**
317      * <p>
318      * Creates a checkbox input tag that use the given name, value and attributes.
319      * The attributes are built from the Map and List given. The create tag is
320      * appended to the StringBuffer given. If the check is to be selected, the
321      * <code>checked</code> parameter should be true.
322      * </p>
323      *
324      * @param buf The StringBuffer to append to
325      * @param name The name of the checkbox tag
326      * @param value The value of the checkbox tag
327      * @param checked Determines whether or not the checked attribute is added
328      * tp the generated tag
329      * @param attributes (optional) A hash of all the attributes with values
330      * to append after the name and value have been appended.
331      * @param singleAttrs (optional) A list of all the valueless attributes to
332      * append at the end of the tag
333      */

334     public static void createCheckboxTag(StringBuffer JavaDoc buf, String JavaDoc id, String JavaDoc name,
335             String JavaDoc value, boolean checked, Map JavaDoc attributes, List JavaDoc singleAttrs) {
336
337         appendInputTagStart(buf, HtmlConstants.CHECKBOX_TYPE, id, name, value);
338         if (checked) {
339             buf.append(" ").append(HtmlConstants.CHECKED);
340         }
341         appendAttributes(buf, attributes, singleAttrs);
342         buf.append("/>");
343     }
344
345     /**
346      * Creates a submit tag with the given name, value and the attributes from
347      * the map and the list.
348      *
349      * @param buf The string buffer to append to
350      * @param name The name of the submit tag
351      * @param value The value of the submit tag
352      * @param attributes (optional) A hash of all the attributes with values
353      * to append after the name and value have been appended.
354      * @param singleAttrs (optional) A list of all the valueless attributes to
355      * append at the end of the tag
356      */

357     public static void createSubmitTag(StringBuffer JavaDoc buf, String JavaDoc id, String JavaDoc name,
358             String JavaDoc value, Map JavaDoc attributes, List JavaDoc singleAttrs) {
359
360         appendInputTagStart(buf, HtmlConstants.SUBMIT_TYPE, id, name, value);
361         appendAttributes(buf, attributes, singleAttrs);
362         buf.append("/>");
363     }
364
365     /**
366      * Creates an image tag with the given name, value and the attributes from
367      * the map and the list.
368      *
369      * @param buf The string buffer to append to
370      * @param name The name of the image tag
371      * @param src The source of the image tag
372      * @param value (optional) The value of the input tag
373      * @param attributes (optional) A hash of all the attributes with values
374      * to append after the name and value have been appended.
375      * @param singleAttrs (optional) A list of all the valueless attributes to
376      * append at the end of the tag
377      */

378     public static void createImageTag(StringBuffer JavaDoc buf, String JavaDoc id, String JavaDoc name,
379             String JavaDoc src, String JavaDoc value, Map JavaDoc attributes, List JavaDoc singleAttrs) {
380
381         appendInputTagStart(buf, HtmlConstants.IMAGE_TYPE, id, name, value);
382         appendAttribute(buf, HtmlConstants.SRC, src);
383         appendAttributes(buf, attributes, singleAttrs);
384         buf.append("/>");
385     }
386
387
388     //-------------------------------------------------------------------------
389
// The non-input form tags
390
//-------------------------------------------------------------------------
391

392
393     /**
394      * Creates a open form tag with the given name, method and attributes from
395      * the Map and List. The action should be supplied in the attributes Map.
396      *
397      * @param buf The string buffer to append to
398      * @param name The name of the form
399      * @param method The method for the form
400      * @param attributes (optional) A hash of all the attributes with values
401      * to append after the name and value have been appended.
402      * @param singleAttrs (optional) A list of all the valueless attributes to
403      * append at the end of the tag
404      */

405     public static void createFormStartTag(StringBuffer JavaDoc buf, String JavaDoc action, String JavaDoc id,
406             String JavaDoc name, String JavaDoc method, Map JavaDoc attributes, List JavaDoc singleAttrs) {
407
408         buf.append("<form");
409         appendAttribute(buf, HtmlConstants.ACTION, action);
410         appendAttribute(buf, HtmlConstants.ID, id);
411         appendAttribute(buf, HtmlConstants.NAME, name);
412         appendAttribute(buf, HtmlConstants.METHOD, method);
413         appendAttributes(buf, attributes, singleAttrs);
414         buf.append(">");
415     }
416
417     /**
418      * Creates a text area tag with the given name, text (as an Object) and
419      * attributes from the Map and List. This is appended to the given
420      * StringBuffer.
421      *
422      * @param buf The string buffer to append to
423      * @param name The name of the textarea tag
424      * @param objectText (optional) The text of the textarea tag in an object.
425      * If the object is not null, then the text of the tag is the
426      * toString of the object
427      * @param attributes (optional) A hash of all the attributes with values
428      * to append after the name and value have been appended.
429      * @param singleAttrs (optional) A list of all the valueless attributes to
430      * append at the end of the tag
431      */

432     public static void createTextAreaTag(StringBuffer JavaDoc buf, String JavaDoc id, String JavaDoc name,
433             Object JavaDoc objectText, Map JavaDoc attributes, List JavaDoc singleAttrs) {
434
435         buf.append("<textarea");
436         appendAttribute(buf, HtmlConstants.ID, id);
437         appendAttribute(buf, HtmlConstants.NAME, name);
438         appendAttributes(buf, attributes, singleAttrs);
439         buf.append(">");
440
441         if (objectText != null) {
442             buf.append(objectText.toString());
443         }
444
445         buf.append(HtmlConstants.TEXTAREA_END_TAG);
446     }
447
448     /**
449      * Creates a textarea tag. The tag has the given name, text and attributes
450      * from the List and Map. These are appended to the given StringBuffer.
451      *
452      * @param buf The StringBuffer to append to
453      * @param name The name of the textarea tag
454      * @param attributes (optional) A hash of all the attributes with values
455      * to append after the name and value have been appended.
456      * @param singleAttrs (optional) A list of all the valueless attributes to
457      * append at the end of the tag
458      */

459     public static void createTextArea(StringBuffer JavaDoc buf, String JavaDoc id, String JavaDoc name,
460             String JavaDoc text, Map JavaDoc attributes, List JavaDoc singleAttrs) {
461
462         buf.append("<textarea");
463         appendAttribute(buf, HtmlConstants.ID, id);
464         appendAttribute(buf, HtmlConstants.NAME, name);
465         appendAttributes(buf, attributes,singleAttrs);
466         buf.append(">").append(text).append("</textarea>");
467     }
468
469     /**
470      * Creates a select start tag with the given name and attributes from the Map
471      * and List. This is appended to the given StringBuffer.
472      *
473      * @param buf The StringBuffer to append to
474      * @param name The name of the select tag
475      * @param attributes (optional) A hash of all the attributes with values
476      * to append after the name and value have been appended.
477      * @param singleAttrs (optional) A list of all the valueless attributes to
478      * append at the end of the tag
479      */

480     public static void createSelectStartTag(StringBuffer JavaDoc buf, String JavaDoc id, String JavaDoc name,
481             Map JavaDoc attributes, List JavaDoc singleAttrs) {
482
483         buf.append("<select");
484         appendAttribute(buf, HtmlConstants.ID, id);
485         appendAttribute(buf, HtmlConstants.NAME, name);
486         appendAttributes(buf, attributes, singleAttrs);
487         buf.append(">");
488     }
489
490     /**
491      * Creates a full option tag with the given value and test, which is appended
492      * between the open and close tags.
493      *
494      * @param buf The StringBuffer to append to
495      * @param id The id of the tag
496      * @param value The value of the option tag
497      * @param text The text of the option tag
498      * @param selected Determines whether or not the selected attribute is
499      * appended to the StringBuffer
500      * @param attributes (optional) A hash of all the attributes with values
501      * to append after the name and value have been appended.
502      * @param singleAttrs (optional) A list of all the valueless attributes to
503      * append at the end of the tag
504      */

505     public static void createOptionTag(StringBuffer JavaDoc buf, String JavaDoc id, String JavaDoc value,
506             String JavaDoc text, boolean selected, Map JavaDoc attributes, List JavaDoc singleAttrs) {
507
508         buf.append("<option");
509         appendAttribute(buf, HtmlConstants.ID, id);
510         appendAttribute(buf, HtmlConstants.VALUE, value);
511         if (selected) {
512             buf.append(" ").append(HtmlConstants.SELECTED);
513         }
514         appendAttributes(buf, attributes, singleAttrs);
515         buf.append(">");
516
517         if (text != null) {
518             buf.append(text);
519         }
520
521         buf.append("</option>");
522     }
523
524     /**
525      * Creates an option start tag with the given value and attributes from the
526      * List and Map. If the option is selected, true must be supplied for the
527      * selected parameter
528      *
529      * @param buf The StringBuffer to append to
530      * @param value The value of the option tag
531      * @param selected Determines whether or not the selected attribute is
532      * appended to the StringBuffer
533      * @param attributes (optional) A hash of all the attributes with values
534      * to append after the name and value have been appended.
535      * @param singleAttrs (optional) A list of all the valueless attributes to
536      * append at the end of the tag
537      */

538     public static void createOptionStartTag(StringBuffer JavaDoc buf, String JavaDoc value,
539             boolean selected, Map JavaDoc attributes, List JavaDoc singleAttrs) {
540
541         buf.append("<option");
542         appendAttribute(buf, "value", value);
543         if (selected) {
544             buf.append(" ").append(HtmlConstants.SELECTED);
545         }
546         appendAttributes(buf, attributes, singleAttrs);
547         buf.append(">");
548     }
549
550
551     //-------------------------------------------------------------------------
552
// The non-form tags
553
//-------------------------------------------------------------------------
554

555
556     /**
557      * Creates an opening anchor tag with the id, name, href and attributes.
558      *
559      * @param buf The StringBuffer to append to
560      * @param id (Optional) The id of the tag
561      * @param name (Optional) The name of the tag
562      * @param href The href of the anchor tag
563      * @param attributes The Map of optional attributes
564      * @param singleAttrs The List of nameless optional attributes
565      */

566     public static void createAnchorTag(StringBuffer JavaDoc buf, String JavaDoc id, String JavaDoc name,
567             String JavaDoc href, Map JavaDoc attributes, List JavaDoc singleAttrs) {
568         buf.append("<a");
569         appendAttribute(buf, HtmlConstants.ID, id);
570         appendAttribute(buf, HtmlConstants.NAME, name);
571         appendAttribute(buf, "href", href);
572         appendAttributes(buf, attributes, singleAttrs);
573         buf.append(">");
574     }
575
576     /**
577      * Creates an img tag with the id, name, src and attributes.
578      *
579      * @param buf The StringBuffer to append to
580      * @param id (Optional) The id of the tag
581      * @param name (Optional) The name of the tag
582      * @param src The src of the tag
583      * @param attributes The Map of optional attributes
584      * @param singleAttrs The List of nameless optional attributes
585      */

586     public static void createImgTag(StringBuffer JavaDoc buf, String JavaDoc id, String JavaDoc name,
587             String JavaDoc src, Map JavaDoc attributes, List JavaDoc singleAttrs) {
588         buf.append("<img");
589         appendAttribute(buf, HtmlConstants.ID, id);
590         appendAttribute(buf, HtmlConstants.NAME, name);
591         appendAttribute(buf, HtmlConstants.SRC, src);
592         appendAttributes(buf, attributes, singleAttrs);
593         buf.append("/>");
594     }
595 }
Popular Tags