1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.webapp;
21
22 import org.apache.myfaces.tobago.internal.util.Deprecation;
23 import org.apache.myfaces.tobago.renderkit.css.CssItem;
24 import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
25 import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
26 import org.apache.myfaces.tobago.renderkit.html.HtmlTypes;
27 import org.apache.myfaces.tobago.renderkit.html.MarkupLanguageAttributes;
28
29 import javax.faces.component.UIComponent;
30 import javax.faces.context.ResponseWriter;
31 import java.io.IOException;
32 import java.io.Writer;
33 import java.nio.charset.StandardCharsets;
34
35
36
37
38
39 public abstract class TobagoResponseWriter extends ResponseWriter {
40
41
42
43
44
45
46 @Deprecated
47 @Override
48 public abstract void startElement(String name, UIComponent component) throws IOException;
49
50 public abstract void startElement(HtmlElements name) throws IOException;
51
52
53
54
55 @Deprecated
56 @Override
57 public abstract void endElement(String name) throws IOException;
58
59 public abstract void endElement(HtmlElements name) throws IOException;
60
61 @Override
62 public abstract void write(String string) throws IOException;
63
64 @Override
65 public abstract void writeComment(Object comment) throws IOException;
66
67 @Override
68 public abstract ResponseWriter cloneWithWriter(Writer writer);
69
70
71
72
73
74 @Override
75 @Deprecated
76 public abstract void writeAttribute(String name, Object value, final String property) throws IOException;
77
78
79
80
81
82 @Override
83 @Deprecated
84 public abstract void writeURIAttribute(String name, Object value, final String property) throws IOException;
85
86
87
88
89 @Override
90 @Deprecated
91 public abstract void writeText(Object text, String property) throws IOException;
92
93 @Override
94 public abstract void flush() throws IOException;
95
96
97
98
99
100
101
102 public abstract void writeAttribute(MarkupLanguageAttributes name, String string, boolean escape) throws IOException;
103
104 public abstract void writeAttribute(MarkupLanguageAttributes name, HtmlTypes type) throws IOException;
105
106
107
108
109 public abstract void writeURIAttribute(MarkupLanguageAttributes name, String string) throws IOException;
110
111
112
113
114 public void writeAttribute(final MarkupLanguageAttributes name, final boolean on) throws IOException {
115 if (on) {
116 writeAttribute(name, name.getValue(), false);
117 }
118 }
119
120
121
122
123 public void writeAttribute(final MarkupLanguageAttributes name, final Integer number) throws IOException {
124 if (number != null) {
125 writeAttribute(name, Integer.toString(number), false);
126 }
127 }
128
129
130
131
132 public void writeIdAttribute(final String id) throws IOException {
133 writeAttribute(HtmlAttributes.ID, id, false);
134 }
135
136
137
138
139 public void writeNameAttribute(final String name) throws IOException {
140 writeAttribute(HtmlAttributes.NAME, name, false);
141 }
142
143
144
145
146
147
148
149 @Deprecated
150 public void writeCommandMapAttribute(final String map) throws IOException {
151 Deprecation.LOG.error("No longer supported. Data: {}", map);
152 }
153
154
155
156
157 public void writeClassAttribute(final CssItem... first) throws IOException {
158 writeClassAttribute(null, null, null, null, null, first);
159 }
160
161
162
163
164 public void writeClassAttribute(final CssItem first, final CssItem[] second, final CssItem... third)
165 throws IOException {
166 writeClassAttribute(first, second, null, null, null, third);
167 }
168
169
170
171
172 public void writeClassAttribute(final CssItem first, final CssItem[] second, final CssItem[] third,
173 final CssItem... fourth) throws IOException {
174 writeClassAttribute(first, second, third, null, null, fourth);
175 }
176
177
178
179
180 public void writeClassAttribute(final CssItem first, final CssItem[] second, final CssItem[] third,
181 final CssItem[] fourth, final CssItem... fifth) throws IOException {
182 writeClassAttribute(first, second, third, fourth, null, fifth);
183 }
184
185 public void writeClassAttribute(final CssItem first, final CssItem[] second, final CssItem[] third,
186 final CssItem[] fourth, final CssItem[] fifth, final CssItem... sixth)
187 throws IOException {
188 final StringBuilder builder = new StringBuilder();
189 boolean render = false;
190 if (first != null) {
191 builder.append(first.getName());
192 builder.append(' ');
193 render = true;
194 }
195 if (second != null) {
196 render |= writeCssItem(builder, second);
197 }
198 if (third != null) {
199 render |= writeCssItem(builder, third);
200 }
201 if (fourth != null) {
202 render |= writeCssItem(builder, fourth);
203 }
204 if (fifth != null) {
205 render |= writeCssItem(builder, fifth);
206 }
207 if (sixth != null) {
208 render |= writeCssItem(builder, sixth);
209 }
210 if (render) {
211 writeAttribute(HtmlAttributes.CLASS, builder.deleteCharAt(builder.length() - 1).toString(), false);
212 }
213 }
214
215 private boolean writeCssItem(final StringBuilder builder, final CssItem... cssItems) {
216 boolean render = false;
217 for (final CssItem cssItem : cssItems) {
218 if (cssItem != null && !"".equals(cssItem.getName())) {
219 builder.append(cssItem.getName());
220 builder.append(' ');
221 render = true;
222 }
223 }
224 return render;
225 }
226
227
228
229
230 public void writeText(final String text) throws IOException {
231 writeText(text, null);
232 }
233
234 public String getContentTypeWithCharSet() {
235 String contentType = getContentType();
236 if (contentType == null) {
237 contentType = "text/html";
238 }
239 String characterEncoding = getCharacterEncoding();
240 if (characterEncoding == null) {
241 characterEncoding = StandardCharsets.UTF_8.name();
242 }
243
244 return contentType + "; charset=" + characterEncoding;
245 }
246
247 @Override
248 public void startCDATA() throws IOException {
249 write("<![CDATA[");
250 }
251
252 @Override
253 public void endCDATA() throws IOException {
254 write("]]>");
255 }
256
257
258 }