1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.shale.remoting.faces; |
19 | |
|
20 | |
import java.io.IOException; |
21 | |
import java.io.Writer; |
22 | |
import javax.faces.component.UIComponent; |
23 | |
import javax.faces.context.ResponseWriter; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
public class BasicResponseWriter extends ResponseWriter { |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 4 | public BasicResponseWriter(Writer writer, String contentType, String characterEncoding) { |
44 | 4 | this.writer = writer; |
45 | 4 | this.contentType = contentType; |
46 | 4 | this.characterEncoding = characterEncoding; |
47 | 4 | } |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | 4 | private String characterEncoding = null; |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | 4 | private String contentType = "text/html"; |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | 4 | private boolean open = false; |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | 4 | private Writer writer = null; |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public ResponseWriter cloneWithWriter(Writer writer) { |
86 | |
return new BasicResponseWriter(writer, contentType, characterEncoding); |
87 | |
} |
88 | |
|
89 | |
|
90 | |
|
91 | |
public void endDocument() throws IOException { |
92 | |
finish(); |
93 | |
writer.flush(); |
94 | |
} |
95 | |
|
96 | |
|
97 | |
|
98 | |
public void endElement(String name) throws IOException { |
99 | 2 | if (open) { |
100 | 2 | writer.write("/"); |
101 | 2 | finish(); |
102 | 2 | } else { |
103 | |
writer.write("</"); |
104 | |
writer.write(name); |
105 | |
writer.write(">"); |
106 | |
} |
107 | 2 | } |
108 | |
|
109 | |
|
110 | |
|
111 | |
public String getCharacterEncoding() { |
112 | |
return this.characterEncoding; |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | |
public String getContentType() { |
118 | |
return this.contentType; |
119 | |
} |
120 | |
|
121 | |
|
122 | |
|
123 | |
public void flush() throws IOException { |
124 | |
finish(); |
125 | |
} |
126 | |
|
127 | |
|
128 | |
|
129 | |
public void startDocument() throws IOException { |
130 | |
|
131 | |
} |
132 | |
|
133 | |
|
134 | |
|
135 | |
public void startElement(String name, UIComponent component) throws IOException { |
136 | 2 | if (name == null) { |
137 | |
throw new NullPointerException(); |
138 | |
} |
139 | 2 | finish(); |
140 | 2 | writer.write('<'); |
141 | 2 | writer.write(name); |
142 | 2 | open = true; |
143 | 2 | } |
144 | |
|
145 | |
|
146 | |
|
147 | |
public void writeAttribute(String name, Object value, String property) throws IOException { |
148 | 3 | if ((name == null) || (value == null)) { |
149 | |
throw new NullPointerException(); |
150 | |
} |
151 | 3 | if (!open) { |
152 | |
throw new IllegalStateException(); |
153 | |
} |
154 | 3 | writer.write(" "); |
155 | 3 | writer.write(name); |
156 | 3 | writer.write("=\""); |
157 | 3 | if (value instanceof String) { |
158 | 3 | string((String) value); |
159 | 3 | } else { |
160 | |
string(value.toString()); |
161 | |
} |
162 | 3 | writer.write("\""); |
163 | 3 | } |
164 | |
|
165 | |
|
166 | |
|
167 | |
public void writeComment(Object comment) throws IOException { |
168 | |
if (comment == null) { |
169 | |
throw new NullPointerException(); |
170 | |
} |
171 | |
finish(); |
172 | |
writer.write("<!-- "); |
173 | |
if (comment instanceof String) { |
174 | |
writer.write((String) comment); |
175 | |
} else { |
176 | |
writer.write(comment.toString()); |
177 | |
} |
178 | |
writer.write(" -->"); |
179 | |
} |
180 | |
|
181 | |
|
182 | |
|
183 | |
public void writeText(Object text, String property) throws IOException { |
184 | 1 | if (text == null) { |
185 | |
throw new NullPointerException(); |
186 | |
} |
187 | 1 | finish(); |
188 | 1 | if (text instanceof String) { |
189 | 1 | string((String) text); |
190 | 1 | } else { |
191 | |
string(text.toString()); |
192 | |
} |
193 | 1 | } |
194 | |
|
195 | |
|
196 | |
|
197 | |
public void writeText(char[] text, int off, int len) throws IOException { |
198 | |
if (text == null) { |
199 | |
throw new NullPointerException(); |
200 | |
} |
201 | |
if ((off < 0) || (off > text.length) || (len < 0) || (len > text.length)) { |
202 | |
throw new IndexOutOfBoundsException(); |
203 | |
} |
204 | |
finish(); |
205 | |
string(text, off, len); |
206 | |
} |
207 | |
|
208 | |
|
209 | |
|
210 | |
public void writeURIAttribute(String name, Object value, String property) throws IOException { |
211 | 2 | if ((name == null) || (value == null)) { |
212 | |
throw new NullPointerException(); |
213 | |
} |
214 | 2 | if (!open) { |
215 | |
throw new IllegalStateException(); |
216 | |
} |
217 | 2 | writer.write(" "); |
218 | 2 | writer.write(name); |
219 | 2 | writer.write("=\""); |
220 | 2 | if (value instanceof String) { |
221 | 2 | string((String) value); |
222 | 2 | } else { |
223 | |
string(value.toString()); |
224 | |
} |
225 | 2 | writer.write("\""); |
226 | 2 | } |
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
public void close() throws IOException { |
234 | |
finish(); |
235 | |
writer.close(); |
236 | |
} |
237 | |
|
238 | |
|
239 | |
|
240 | |
public void write(char[] cbuf, int off, int len) throws IOException { |
241 | 12 | finish(); |
242 | 12 | writer.write(cbuf, off, len); |
243 | 12 | } |
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
private void character(char ch) throws IOException { |
257 | |
|
258 | 130 | if (ch <= 0xff) { |
259 | |
|
260 | |
|
261 | 130 | if (ch == 0x22) { |
262 | |
writer.write("""); |
263 | |
} else if (ch == 0x26) { |
264 | |
writer.write("&"); |
265 | |
} else if (ch == 0x27) { |
266 | |
writer.write("'"); |
267 | |
} else if (ch == 0x3C) { |
268 | |
writer.write("<"); |
269 | |
} else if (ch == 0X3E) { |
270 | |
writer.write(">"); |
271 | |
} else { |
272 | 130 | writer.write(ch); |
273 | |
} |
274 | 130 | } else { |
275 | |
if (substitution()) { |
276 | |
numeric(writer, ch); |
277 | |
} else { |
278 | |
writer.write(ch); |
279 | |
} |
280 | |
} |
281 | |
|
282 | 130 | } |
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
private void finish() throws IOException { |
291 | |
|
292 | 17 | if (open) { |
293 | 2 | writer.write(">"); |
294 | 2 | open = false; |
295 | |
} |
296 | |
|
297 | 17 | } |
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
private void numeric(Writer writer, char ch) throws IOException { |
310 | |
|
311 | |
writer.write("&#"); |
312 | |
writer.write(String.valueOf((int) ch)); |
313 | |
writer.write(";"); |
314 | |
|
315 | |
} |
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
private void string(char[] text, int off, int len) throws IOException { |
329 | |
|
330 | |
|
331 | |
for (int i = off; i < (off + len); i++) { |
332 | |
character(text[i]); |
333 | |
} |
334 | |
|
335 | |
} |
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
private void string(String s) throws IOException { |
347 | |
|
348 | 136 | for (int i = 0; i < s.length(); i++) { |
349 | 130 | character(s.charAt(i)); |
350 | |
} |
351 | |
|
352 | 6 | } |
353 | |
|
354 | |
|
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
private boolean substitution() { |
360 | |
|
361 | |
return !("UTF-8".equals(characterEncoding) || "UTF-16".equals(characterEncoding)); |
362 | |
|
363 | |
} |
364 | |
|
365 | |
|
366 | |
} |