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.config.TobagoConfig;
23 import org.apache.myfaces.tobago.renderkit.css.Classes;
24 import org.apache.myfaces.tobago.renderkit.css.Style;
25 import org.apache.myfaces.tobago.renderkit.html.DataAttributes;
26 import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
27 import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
28 import org.apache.myfaces.tobago.renderkit.html.StyleClasses;
29
30 import javax.faces.component.UIComponent;
31 import javax.faces.context.FacesContext;
32 import javax.faces.context.ResponseWriter;
33 import java.io.IOException;
34 import java.io.Writer;
35
36
37
38
39
40
41 public abstract class TobagoResponseWriter extends ResponseWriter {
42
43
44
45 @Override
46 public abstract void startElement(String name, UIComponent component) throws IOException;
47
48
49
50
51 @Deprecated
52 public void startElement(String name) throws IOException {
53 startElement(name, null);
54 }
55
56 @Override
57 public abstract void endElement(String name) throws IOException;
58
59 public abstract void write(String string) throws IOException;
60
61 @Override
62 public abstract void writeComment(Object comment) throws IOException;
63
64 @Override
65 public abstract ResponseWriter cloneWithWriter(Writer writer);
66
67
68
69
70 @Deprecated
71 public abstract void writeAttribute(String name, Object value, final String property) throws IOException;
72
73
74
75
76 @Deprecated
77 public abstract void writeText(Object text, String property) throws IOException;
78
79 @Override
80 public abstract void flush() throws IOException;
81
82
83
84
85
86
87
88 public abstract void writeAttribute(String name, String string, boolean escape) throws IOException;
89
90
91
92
93 public void writeAttribute(String name, boolean on) throws IOException {
94 if (on) {
95 writeAttribute(name, name, false);
96 }
97 }
98
99
100
101
102 public void writeAttribute(String name, int number) throws IOException {
103 writeAttribute(name, Integer.toString(number), false);
104 }
105
106
107
108
109 public void writeAttributeFromComponent(String name, String property) throws IOException {
110 writeAttribute(name, null, property);
111 }
112
113
114
115
116 public void writeIdAttribute(String id) throws IOException {
117 writeAttribute(HtmlAttributes.ID, id, false);
118 }
119
120
121
122
123 public void writeNameAttribute(String name) throws IOException {
124 writeAttribute(HtmlAttributes.NAME, name, false);
125 }
126
127
128
129
130
131 @Deprecated
132 public void writeClassAttribute(String cssClass) throws IOException {
133 writeAttribute(HtmlAttributes.CLASS, cssClass, false);
134 }
135
136
137
138
139
140 @Deprecated
141 public void writeClassAttribute(StyleClasses styleClasses) throws IOException {
142 writeAttribute(HtmlAttributes.CLASS, styleClasses.toString(), false);
143 }
144
145
146
147
148
149
150
151
152
153 public void writeClassAttribute(Classes classes) throws IOException {
154 String styleClasses = getStyleClasses();
155 String stringValue = classes.getStringValue();
156 if (styleClasses != null) {
157 stringValue += " " + styleClasses;
158 }
159 writeAttribute(HtmlAttributes.CLASS, stringValue, false);
160 }
161
162 @Deprecated
163 public abstract String getStyleClasses();
164
165
166
167
168
169 @Deprecated
170 public abstract void writeClassAttribute() throws IOException;
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189 public void writeStyleAttribute(Style style) throws IOException {
190 if (style != null) {
191 if (TobagoConfig.getInstance(FacesContext.getCurrentInstance()).getContentSecurityPolicy().size() > 0) {
192
193
194
195 final String json = style.encodeJson();
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 if (json.length() > 2) {
212 writeAttribute(DataAttributes.STYLE, json, true);
213 }
214 } else {
215
216 final String value = style.encode();
217 if (value.length() > 0) {
218 writeAttribute(HtmlAttributes.STYLE, value, style.needsToBeEscaped());
219 }
220 }
221 }
222 }
223
224
225
226
227
228 @Deprecated
229 public void writeStyleAttribute(String style) throws IOException {
230 writeAttribute(HtmlAttributes.STYLE, style, false);
231 }
232
233 public void writeJavascript(String script) throws IOException {
234 startJavascript();
235 write(script);
236 endJavascript();
237 }
238
239 public void endJavascript() throws IOException {
240
241 endElement(HtmlElements.SCRIPT);
242 }
243
244 public void startJavascript() throws IOException {
245 startElement(HtmlElements.SCRIPT, null);
246 writeAttribute(HtmlAttributes.TYPE, "text/javascript", false);
247 flush();
248
249 }
250
251
252
253
254 public void writeText(String text) throws IOException {
255 writeText(text, null);
256 }
257
258
259
260
261 public void writeTextFromComponent(String property) throws IOException {
262 writeText(null, property);
263 }
264
265 public String getContentTypeWithCharSet() {
266 String contentType = getContentType();
267 if (contentType == null) {
268 contentType = "text/html";
269 }
270 String characterEncoding = getCharacterEncoding();
271 if (characterEncoding == null) {
272 characterEncoding = "UTF-8";
273 }
274
275 StringBuilder builder = new StringBuilder(contentType);
276 builder.append(";charset=");
277 builder.append(characterEncoding);
278 return builder.toString();
279
280 }
281 }