1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package net.sf.bimbo.impl;
19
20 import java.io.PrintWriter;
21 import java.util.LinkedList;
22 import java.util.List;
23
24
25
26
27
28
29
30 public class HtmlElement {
31
32 private final String elementName;
33
34 private String escapedBody;
35
36
37
38
39
40
41
42 public HtmlElement(String elementName) {
43 this.elementName = elementName;
44 this.elements = new LinkedList<Element>();
45 }
46
47
48
49
50
51
52
53 public HtmlElement setBody(String body) {
54 this.escapedBody = HtmlUtil.escape(body);
55 return this;
56 }
57
58 private static class Element {
59 private final String name;
60 private final String value;
61
62 public Element(String name, String value) {
63 this.name = name;
64 this.value = value;
65 }
66 }
67
68 private final List<Element> elements;
69
70
71
72
73
74
75
76
77
78
79 public HtmlElement addAttribute(String name, String value) {
80 this.elements.add(new Element(name, value));
81 return this;
82 }
83
84
85
86
87
88
89 public void write(PrintWriter writer) {
90 writer.print("<" + this.elementName);
91 for (Element element : this.elements) {
92 String value = element.value;
93 if (null == value) {
94 value = "";
95 }
96 writer.print(" " + element.name + "=\"" + HtmlUtil.escape(value)
97 + "\"");
98 }
99 if (null != this.escapedBody) {
100 writer.println(">");
101 writer.println(this.escapedBody);
102 writer.println("</" + this.elementName + ">");
103 } else {
104 writer.println("/>");
105 }
106 }
107
108
109
110
111
112
113
114 public HtmlElement setEscapedBody(String escapedBody) {
115 this.escapedBody = escapedBody;
116 return this;
117 }
118 }