View Javadoc

1   /*
2    * Copyright 2004 Jeff Johnston
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.extremecomponents.util;
17  
18  import org.apache.commons.lang.StringUtils;
19  
20  
21  /***
22   * @author Jeff Johnston
23   */
24  public class HtmlBuilder
25  {
26  	StringBuffer sb = new StringBuffer();
27  
28  	/***
29  	 * 
30  	 */
31  	public HtmlBuilder format(int tabs, int newlines)
32  	{
33  		 tabs(tabs);
34  		 newlines(newlines);
35  		 
36  		return this;
37  	}
38  	
39  	/***
40  	 * 
41  	 */
42  	public HtmlBuilder tabs(int tabs)
43  	{
44  		for (int i = 0; i < tabs; i++)
45  		{
46  			tab();
47  		}
48  		
49  		return this;
50  	}
51  	
52  	/***
53  	 * 
54  	 */
55  	public HtmlBuilder newlines(int newlines)
56  	{
57  		for (int i = 0; i < newlines; i++)
58  		{
59  			newline();
60  		}
61  		
62  		return this;
63  	}
64  
65  	/***
66  	 * 
67  	 */
68  	public HtmlBuilder tab()
69  	{
70  		sb.append("\t");
71  
72  		return this;
73  	}
74  	
75  	/***
76  	 * 
77  	 */
78  	public HtmlBuilder newline()
79  	{
80  		sb.append("\n");
81  
82  		return this;
83  	}
84  
85  	/***
86  	 * 
87  	 */
88  	public HtmlBuilder append(Object append)
89  	{
90  		sb.append(append);
91  		
92  		return this;
93  	}
94  	
95  	/***
96  	 * 
97  	 */
98  	public HtmlBuilder close()
99  	{
100 		sb.append(">");
101 
102 		return this;
103 	}
104 
105 	/***
106 	 * 
107 	 */
108 	public HtmlBuilder table(int tabs)
109 	{
110 		newline();
111 		tabs(tabs);
112 		sb.append("<table");
113 
114 		return this;
115 	}
116 	
117 	/***
118 	 * 
119 	 */
120 	public HtmlBuilder tableEnd(int tabs)
121 	{
122 		newline();
123 		tabs(tabs);
124 		sb.append("</table>");
125 
126 		return this;
127 	}
128 
129 	/***
130 	 * 
131 	 */
132 	public HtmlBuilder tr(int tabs)
133 	{
134 		newline();
135 		tabs(tabs);
136 		sb.append("<tr");
137 
138 		return this;
139 	}
140 
141 	/***
142 	 * 
143 	 */
144 	public HtmlBuilder trEnd(int tabs)
145 	{
146 		newline();
147 		tabs(tabs);
148 		sb.append("</tr>");
149 
150 		return this;
151 	}
152 
153 	/***
154 	 * 
155 	 */
156 	public HtmlBuilder th(int tabs)
157 	{
158 		newline();
159 		tabs(tabs);
160 		sb.append("<th");
161 
162 		return this;
163 	}
164 
165 	/***
166 	 * 
167 	 */
168 	public HtmlBuilder thEnd()
169 	{
170 		sb.append("</th>");
171 
172 		return this;
173 	}
174 
175 	/***
176 	 * 
177 	 */
178 	public HtmlBuilder td(int tabs)
179 	{
180 		newline();
181 		tabs(tabs);
182 		sb.append("<td");
183 
184 		return this;
185 	}
186 
187 	/***
188 	 * 
189 	 */
190 	public HtmlBuilder tdEnd()
191 	{
192 		sb.append("</td>");
193 
194 		return this;
195 	}
196 
197 	/***
198 	 * 
199 	 */
200 	public HtmlBuilder input(String type)
201 	{
202 		sb.append("<input type=\"" + type + "\" ");
203 
204 		return this;
205 	}
206 
207 	/***
208 	 * 
209 	 */
210 	public HtmlBuilder select()
211 	{
212 		sb.append("<select");
213 
214 		return this;
215 	}
216 
217 	/***
218 	 * 
219 	 */
220 	public HtmlBuilder selectEnd()
221 	{
222 		sb.append("</select>");
223 
224 		return this;
225 	}
226 
227 	/***
228 	 * 
229 	 */
230 	public HtmlBuilder option()
231 	{
232 		sb.append("<option");
233 
234 		return this;
235 	}
236 
237 	/***
238 	 * 
239 	 */
240 	public HtmlBuilder optionEnd()
241 	{
242 		sb.append("</option>");
243 
244 		return this;
245 	}
246 
247 	/***
248 	 * 
249 	 */
250 	public HtmlBuilder form()
251 	{
252 		newline();
253 		sb.append("<form");
254 
255 		return this;
256 	}
257 
258 	/***
259 	 * 
260 	 */
261 	public HtmlBuilder formEnd()
262 	{
263 		newline();
264 		sb.append("</form>");
265 
266 		return this;
267 	}
268 
269 	/***
270 	 * 
271 	 */
272 	public HtmlBuilder name(String name)
273 	{
274 		if (StringUtils.isNotBlank(name))
275 		{
276 			sb.append(" name=\"" + name + "\" ");
277 		}
278 
279 		return this;
280 	}
281 
282 	/***
283 	 * 
284 	 */
285 	public HtmlBuilder value(String value)
286 	{
287 		if (StringUtils.isNotBlank(value))
288 		{
289 			sb.append(" value=\"" + value + "\" ");
290 		}
291 
292 		return this;
293 	}
294 
295 	/***
296 	 * 
297 	 */
298 	public HtmlBuilder title(String title)
299 	{
300 		if (StringUtils.isNotBlank(title))
301 		{
302 			sb.append(" title=\"" + title + "\" ");
303 		}
304 
305 		return this;
306 	}
307 
308 	/***
309 	 * 
310 	 */
311 	public HtmlBuilder action(String action)
312 	{
313 		if (StringUtils.isNotBlank(action))
314 		{
315 			sb.append(" action=\"" + action + "\" ");
316 		}
317 
318 		return this;
319 	}
320 
321 	/***
322 	 * 
323 	 */
324 	public HtmlBuilder method(String method)
325 	{
326 		if (StringUtils.isNotBlank(method))
327 		{
328 			sb.append(" method=\"" + method + "\" ");
329 		}
330 
331 		return this;
332 	}
333 
334 	/***
335 	 * 
336 	 */
337 	public HtmlBuilder onsubmit(String onsubmit)
338 	{
339 		if (StringUtils.isNotBlank(onsubmit))
340 		{
341 			sb.append(" onsubmit=\"" + onsubmit + "\" ");
342 		}
343 
344 		return this;
345 	}
346 
347 	/***
348 	 * 
349 	 */
350 	public HtmlBuilder onclick(String onclick)
351 	{
352 		if (StringUtils.isNotBlank(onclick))
353 		{
354 			sb.append(" onclick=\"" + onclick + "\" ");
355 		}
356 
357 		return this;
358 	}
359 
360 	/***
361 	 * 
362 	 */
363 	public HtmlBuilder onchange(String onchange)
364 	{
365 		if (StringUtils.isNotBlank(onchange))
366 		{
367 			sb.append(" onchange=\"" + onchange + "\" ");
368 		}
369 
370 		return this;
371 	}
372 
373 	/***
374 	 * 
375 	 */
376 	public HtmlBuilder id(String id)
377 	{
378 		if (StringUtils.isNotBlank(id))
379 		{
380 			sb.append(" id=\"" + id + "\" ");
381 		}
382 
383 		return this;
384 	}
385 
386 	/***
387 	 * 
388 	 */
389 	public HtmlBuilder styleClass(String styleClass)
390 	{
391 		if (StringUtils.isNotBlank(styleClass))
392 		{
393 			sb.append(" class=\"" + styleClass + "\" ");
394 		}
395 
396 		return this;
397 	}
398 
399 	/***
400 	 * 
401 	 */
402 	public HtmlBuilder style(String style)
403 	{
404 		if (StringUtils.isNotBlank(style))
405 		{
406 			sb.append(" style=" + "\"" + style + "\" ");
407 		}
408 
409 		return this;
410 	}
411 
412 	/***
413 	 * 
414 	 */
415 	public HtmlBuilder width(String width)
416 	{
417 		if (StringUtils.isNotBlank(width))
418 		{
419 			sb.append(" width=\"" + width + "\" ");
420 		}
421 
422 		return this;
423 	}
424 
425 	/***
426 	 * 
427 	 */
428 	public HtmlBuilder align(String align)
429 	{
430 		if (StringUtils.isNotBlank(align))
431 		{
432 			sb.append(" align=" + "\"" + align + "\" ");
433 		}
434 
435 		return this;
436 	}
437 
438 	/***
439 	 * 
440 	 */
441 	public HtmlBuilder valign(String valign)
442 	{
443 		if (StringUtils.isNotBlank(valign))
444 		{
445 			sb.append(" valign=" + "\"" + valign + "\" ");
446 		}
447 
448 		return this;
449 	}
450 
451 	/***
452 	 * 
453 	 */
454 	public HtmlBuilder border(String border)
455 	{
456 		if (StringUtils.isNotBlank(border))
457 		{
458 			sb.append(" border=" + "\"" + border + "\" ");
459 		}
460 
461 		return this;
462 	}
463 
464 	/***
465 	 * 
466 	 */
467 	public HtmlBuilder cellPadding(String cellPadding)
468 	{
469 		if (StringUtils.isNotBlank(cellPadding))
470 		{
471 			sb.append(" cellpadding=" + "\"" + cellPadding + "\" ");
472 		}
473 
474 		return this;
475 	}
476 
477 	/***
478 	 * 
479 	 */
480 	public HtmlBuilder cellSpacing(String cellSpacing)
481 	{
482 		if (StringUtils.isNotBlank(cellSpacing))
483 		{
484 			sb.append(" cellspacing=" + "\"" + cellSpacing + "\" ");
485 		}
486 
487 		return this;
488 	}
489 
490 	/***
491 	 * 
492 	 */
493 	public HtmlBuilder colSpan(String colspan)
494 	{
495 		if (StringUtils.isNotBlank(colspan))
496 		{
497 			sb.append(" colspan=" + "\"" + colspan + "\" ");
498 		}
499 
500 		return this;
501 	}
502 
503 	/***
504 	 * 
505 	 */
506 	public HtmlBuilder span()
507 	{
508 		sb.append("<span");
509 
510 		return this;
511 	}
512 
513 	/***
514 	 * 
515 	 */
516 	public HtmlBuilder spanEnd()
517 	{
518 		sb.append("</span>");
519 
520 		return this;
521 	}
522 
523 	/***
524 	 * 
525 	 */
526 	public HtmlBuilder div()
527 	{
528 		sb.append("<div");
529 
530 		return this;
531 	}
532 
533 	/***
534 	 * 
535 	 */
536 	public HtmlBuilder divEnd()
537 	{
538 		sb.append("</div>");
539 
540 		return this;
541 	}
542 
543 	/***
544 	 * 
545 	 */
546 	public HtmlBuilder a(String href)
547 	{
548 		append("<a href=");
549 		quote();
550 		append(href);
551 		quote();
552 
553 		return this;
554 	}
555 
556 	/***
557 	 * 
558 	 */
559 	public HtmlBuilder a()
560 	{
561 		sb.append("<a href=");
562 
563 		return this;
564 	}
565 
566 	/***
567 	 * 
568 	 */
569 	public HtmlBuilder aEnd()
570 	{
571 		sb.append("</a>");
572 
573 		return this;
574 	}
575 
576 	/***
577 	 * 
578 	 */
579 	public HtmlBuilder bold()
580 	{
581 		sb.append("<b>");
582 
583 		return this;
584 	}
585 
586 	/***
587 	 * 
588 	 */
589 	public HtmlBuilder boldEnd()
590 	{
591 		sb.append("</b>");
592 
593 		return this;
594 	}
595 
596 	/***
597 	 * 
598 	 */
599 	public HtmlBuilder quote()
600 	{
601 		sb.append("\"");
602 
603 		return this;
604 	}
605 
606 	/***
607 	 * 
608 	 */
609 	public HtmlBuilder question()
610 	{
611 		sb.append("?");
612 
613 		return this;
614 	}
615 
616 	/***
617 	 * 
618 	 */
619 	public HtmlBuilder equals()
620 	{
621 		sb.append("=");
622 
623 		return this;
624 	}
625 
626 	/***
627 	 * 
628 	 */
629 	public HtmlBuilder ampersand()
630 	{
631 		sb.append("&");
632 
633 		return this;
634 	}
635 
636 	/***
637 	 * 
638 	 */
639 	public HtmlBuilder img(String src)
640 	{
641 		sb.append("<img src=" + "\"" + src + "\" style=\"border:0\">");
642 
643 		return this;
644 	}
645 
646 	/***
647 	 * 
648 	 */
649 	public HtmlBuilder img(String img, String title)
650 	{
651 		sb.append("<img src=" + "\"" + img + "\" style=\"border:0\" title=\"" + title + "\">");
652 
653 		return this;
654 	}
655 
656 	/***
657 	 * 
658 	 */
659 	public static String getImage(String img, String title)
660 	{
661 		return "<img src=" + "\"" + img + "\" style=\"border:0\" title=\"" + title + "\">";
662 	}
663 	
664 	/***
665 	 * 
666 	 */
667 	public HtmlBuilder textarea()
668 	{
669 		sb.append("<textarea");
670 
671 		return this;
672 	}
673 
674 	/***
675 	 * 
676 	 */
677 	public HtmlBuilder textareaEnd()
678 	{
679 		sb.append("</textarea>");
680 
681 		return this;
682 	}
683 
684 	/***
685 	 * 
686 	 */
687 	public HtmlBuilder cols(String cols)
688 	{
689 		if (StringUtils.isNotBlank(cols))
690 		{
691 			sb.append(" cols=" + "\"" + cols + "\" ");
692 		}
693 
694 		return this;
695 	}
696 
697 	/***
698 	 * 
699 	 */
700 	public HtmlBuilder rows(String rows)
701 	{
702 		if (StringUtils.isNotBlank(rows))
703 		{
704 			sb.append(" rows=" + "\"" + rows + "\" ");
705 		}
706 
707 		return this;
708 	}
709 
710 	public HtmlBuilder checked()
711 	{
712 		sb.append(" checked=\"checked\"");
713 
714 		return this;
715 	}
716 
717 	public HtmlBuilder nbsp()
718 	{
719 		sb.append("&nbsp;");
720 
721 		return this;
722 	}
723 	
724 	/***
725 	 * 
726 	 */
727 	public HtmlBuilder comment(String comment)
728 	{
729 		if (StringUtils.isNotBlank(comment))
730 		{
731 			sb.append(" <!-- " +  comment + " -->");
732 		}
733 
734 		return this;
735 	}
736 
737 	/***
738 	 * 
739 	 */
740 	public HtmlBuilder ul()
741 	{
742 		sb.append("<ul>");
743 
744 		return this;
745 	}
746 
747 	/***
748 	 * 
749 	 */
750 	public HtmlBuilder ulEnd()
751 	{
752 		sb.append("</ul>");
753 
754 		return this;
755 	}
756 
757 	/***
758 	 * 
759 	 */
760 	public HtmlBuilder li(String text)
761 	{
762 		if (StringUtils.isNotBlank(text))
763 		{
764 			sb.append("<li>" +  text + "</li>");
765 		}
766 
767 		return this;
768 	}
769 
770 	/***
771 	 * 
772 	 */
773 	public HtmlBuilder br()
774 	{
775 		sb.append("<br>");
776 
777 		return this;
778 	}
779 
780 	/***
781 	 * 
782 	 */
783 	public HtmlBuilder maxlength(String maxlength)
784 	{
785 		if (StringUtils.isNotBlank(maxlength))
786 		{
787 			sb.append(" maxlength=" + "\"" + maxlength + "\" ");
788 		}
789 
790 		return this;
791 	}
792 
793 	/***
794 	 * 
795 	 */
796 	public String toString()
797 	{
798 		return sb.toString();
799 	}
800 }