1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.markup; |
16 |
|
|
17 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
18 |
|
import org.apache.hivemind.util.Defense; |
19 |
|
import org.apache.tapestry.IMarkupWriter; |
20 |
|
import org.apache.tapestry.NestedMarkupWriter; |
21 |
|
|
22 |
|
import java.io.PrintWriter; |
23 |
|
import java.util.*; |
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
public class MarkupWriterImpl implements IMarkupWriter |
34 |
|
{ |
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
private PrintWriter _writer; |
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
private MarkupFilter _filter; |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
109 |
private boolean _openTag = false; |
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
109 |
private boolean _emptyTag = false; |
63 |
|
|
64 |
|
private String _contentType; |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
private List _activeElementStack; |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
109 |
private final Map _attrMap = new LinkedHashMap(); |
80 |
|
|
81 |
|
public MarkupWriterImpl(String contentType, PrintWriter writer, MarkupFilter filter) |
82 |
109 |
{ |
83 |
109 |
Defense.notNull(contentType, "contentType"); |
84 |
109 |
Defense.notNull(writer, "writer"); |
85 |
109 |
Defense.notNull(filter, "filter"); |
86 |
|
|
87 |
109 |
_contentType = contentType; |
88 |
109 |
_writer = writer; |
89 |
109 |
_filter = filter; |
90 |
109 |
} |
91 |
|
|
92 |
|
public void attribute(String name, int value) |
93 |
|
{ |
94 |
4 |
checkTagOpen(); |
95 |
|
|
96 |
3 |
_attrMap.put(name, new DefaultAttribute(String.valueOf(value), false)); |
97 |
3 |
} |
98 |
|
|
99 |
|
public void attribute(String name, boolean value) |
100 |
|
{ |
101 |
3 |
checkTagOpen(); |
102 |
|
|
103 |
2 |
_attrMap.put(name, new DefaultAttribute(String.valueOf(value), false)); |
104 |
2 |
} |
105 |
|
|
106 |
|
public void attribute(String name, String value) |
107 |
|
{ |
108 |
193 |
attribute(name, value, false); |
109 |
192 |
} |
110 |
|
|
111 |
|
public void attribute(String name, String value, boolean raw) |
112 |
|
{ |
113 |
193 |
checkTagOpen(); |
114 |
|
|
115 |
192 |
_attrMap.put(name, new DefaultAttribute(value, raw)); |
116 |
192 |
} |
117 |
|
|
118 |
|
public void appendAttribute(String name, boolean value) |
119 |
|
{ |
120 |
1 |
checkTagOpen(); |
121 |
|
|
122 |
1 |
appendAttribute(name, String.valueOf(value)); |
123 |
1 |
} |
124 |
|
|
125 |
|
public void appendAttribute(String name, int value) |
126 |
|
{ |
127 |
0 |
checkTagOpen(); |
128 |
|
|
129 |
0 |
appendAttribute(name, String.valueOf(value)); |
130 |
0 |
} |
131 |
|
|
132 |
|
public void appendAttribute(String name, String value) |
133 |
|
{ |
134 |
8 |
checkTagOpen(); |
135 |
|
|
136 |
8 |
DefaultAttribute attr = (DefaultAttribute)_attrMap.get(name); |
137 |
|
|
138 |
8 |
if (attr == null) |
139 |
|
{ |
140 |
6 |
attr = new DefaultAttribute(value, false); |
141 |
|
|
142 |
6 |
_attrMap.put(name, attr); |
143 |
6 |
return; |
144 |
|
} |
145 |
|
|
146 |
2 |
attr.append(value); |
147 |
2 |
} |
148 |
|
|
149 |
|
public void appendAttributeRaw(String name, String value) |
150 |
|
{ |
151 |
2 |
checkTagOpen(); |
152 |
|
|
153 |
2 |
DefaultAttribute attr = (DefaultAttribute)_attrMap.get(name); |
154 |
|
|
155 |
2 |
if (attr == null) { |
156 |
2 |
attr = new DefaultAttribute(value, true); |
157 |
|
|
158 |
2 |
_attrMap.put(name, attr); |
159 |
2 |
return; |
160 |
|
} |
161 |
|
|
162 |
0 |
attr.setRaw(true); |
163 |
0 |
attr.append(value); |
164 |
0 |
} |
165 |
|
|
166 |
|
public Attribute getAttribute(String name) |
167 |
|
{ |
168 |
3 |
checkTagOpen(); |
169 |
|
|
170 |
3 |
return (Attribute)_attrMap.get(name); |
171 |
|
} |
172 |
|
|
173 |
|
public boolean hasAttribute(String name) |
174 |
|
{ |
175 |
6 |
checkTagOpen(); |
176 |
|
|
177 |
6 |
return _attrMap.containsKey(name); |
178 |
|
} |
179 |
|
|
180 |
|
public void clearAttributes() |
181 |
|
{ |
182 |
1 |
checkTagOpen(); |
183 |
|
|
184 |
1 |
_attrMap.clear(); |
185 |
1 |
} |
186 |
|
|
187 |
|
public Attribute removeAttribute(String name) |
188 |
|
{ |
189 |
1 |
checkTagOpen(); |
190 |
|
|
191 |
1 |
return (Attribute)_attrMap.remove(name); |
192 |
|
} |
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
|
private void maybePrintFiltered(char[] data, int offset, int length, boolean raw, boolean isAttribute) |
199 |
|
{ |
200 |
135 |
if (data == null || length <= 0) |
201 |
3 |
return; |
202 |
|
|
203 |
132 |
if (raw) |
204 |
|
{ |
205 |
84 |
_writer.write(data, offset, length); |
206 |
84 |
return; |
207 |
|
} |
208 |
|
|
209 |
48 |
_filter.print(_writer, data, offset, length, isAttribute); |
210 |
48 |
} |
211 |
|
|
212 |
|
public void attributeRaw(String name, String value) |
213 |
|
{ |
214 |
0 |
attribute(name, value, true); |
215 |
0 |
} |
216 |
|
|
217 |
|
public void begin(String name) |
218 |
|
{ |
219 |
103 |
if (_openTag) |
220 |
18 |
closeTag(); |
221 |
|
|
222 |
103 |
push(name); |
223 |
|
|
224 |
103 |
_writer.print('<'); |
225 |
103 |
_writer.print(name); |
226 |
|
|
227 |
103 |
_openTag = true; |
228 |
103 |
_emptyTag = false; |
229 |
103 |
} |
230 |
|
|
231 |
|
public void beginEmpty(String name) |
232 |
|
{ |
233 |
25 |
if (_openTag) |
234 |
14 |
closeTag(); |
235 |
|
|
236 |
25 |
_writer.print('<'); |
237 |
25 |
_writer.print(name); |
238 |
|
|
239 |
25 |
_openTag = true; |
240 |
25 |
_emptyTag = true; |
241 |
25 |
} |
242 |
|
|
243 |
|
public boolean checkError() |
244 |
|
{ |
245 |
0 |
return _writer.checkError(); |
246 |
|
} |
247 |
|
|
248 |
|
public void close() |
249 |
|
{ |
250 |
9 |
if (_openTag) |
251 |
1 |
closeTag(); |
252 |
|
|
253 |
|
|
254 |
|
|
255 |
15 |
while (!stackEmpty()) |
256 |
|
{ |
257 |
6 |
_writer.print("</"); |
258 |
6 |
_writer.print(pop()); |
259 |
6 |
_writer.print('>'); |
260 |
|
} |
261 |
|
|
262 |
9 |
_writer.close(); |
263 |
|
|
264 |
9 |
_writer = null; |
265 |
9 |
_filter = null; |
266 |
9 |
_activeElementStack = null; |
267 |
9 |
} |
268 |
|
|
269 |
|
public void closeTag() |
270 |
|
{ |
271 |
127 |
flushAttributes(); |
272 |
|
|
273 |
127 |
if (_emptyTag) |
274 |
24 |
_writer.print(" /"); |
275 |
|
|
276 |
127 |
_writer.print('>'); |
277 |
|
|
278 |
127 |
_openTag = false; |
279 |
127 |
_emptyTag = false; |
280 |
127 |
} |
281 |
|
|
282 |
|
|
283 |
|
|
284 |
|
|
285 |
|
|
286 |
|
void flushAttributes() |
287 |
|
{ |
288 |
127 |
if (_attrMap.size() > 0) { |
289 |
|
|
290 |
89 |
Iterator it = _attrMap.keySet().iterator(); |
291 |
289 |
while (it.hasNext()) { |
292 |
|
|
293 |
200 |
String key = (String)it.next(); |
294 |
200 |
DefaultAttribute attr = (DefaultAttribute)_attrMap.get(key); |
295 |
|
|
296 |
200 |
attr.print(key, _writer, _filter); |
297 |
200 |
} |
298 |
|
|
299 |
89 |
_attrMap.clear(); |
300 |
|
} |
301 |
|
|
302 |
127 |
} |
303 |
|
|
304 |
|
public void comment(String value) |
305 |
|
{ |
306 |
2 |
if (_openTag) |
307 |
1 |
closeTag(); |
308 |
|
|
309 |
2 |
_writer.print("<!-- "); |
310 |
2 |
_writer.print(value); |
311 |
2 |
_writer.println(" -->"); |
312 |
2 |
} |
313 |
|
|
314 |
|
public void end() |
315 |
|
{ |
316 |
80 |
if (_openTag) |
317 |
24 |
closeTag(); |
318 |
|
|
319 |
80 |
if (stackEmpty()) |
320 |
1 |
throw new ApplicationRuntimeException(MarkupMessages.endWithEmptyStack()); |
321 |
|
|
322 |
79 |
_writer.print("</"); |
323 |
79 |
_writer.print(pop()); |
324 |
79 |
_writer.print('>'); |
325 |
79 |
} |
326 |
|
|
327 |
|
public void end(String name) |
328 |
|
{ |
329 |
7 |
if (_openTag) |
330 |
3 |
closeTag(); |
331 |
|
|
332 |
7 |
if (_activeElementStack == null || !_activeElementStack.contains(name)) |
333 |
1 |
throw new ApplicationRuntimeException(MarkupMessages.elementNotOnStack( |
334 |
|
name, |
335 |
|
_activeElementStack)); |
336 |
|
|
337 |
|
while (true) |
338 |
|
{ |
339 |
8 |
String tagName = pop(); |
340 |
|
|
341 |
8 |
_writer.print("</"); |
342 |
8 |
_writer.print(tagName); |
343 |
8 |
_writer.print('>'); |
344 |
|
|
345 |
8 |
if (tagName.equals(name)) |
346 |
6 |
break; |
347 |
2 |
} |
348 |
6 |
} |
349 |
|
|
350 |
|
public void flush() |
351 |
|
{ |
352 |
5 |
_writer.flush(); |
353 |
5 |
} |
354 |
|
|
355 |
|
public NestedMarkupWriter getNestedWriter() |
356 |
|
{ |
357 |
8 |
return new NestedMarkupWriterImpl(this, _filter); |
358 |
|
} |
359 |
|
|
360 |
|
public void print(char[] data, int offset, int length) |
361 |
|
{ |
362 |
2 |
print(data, offset, length, false); |
363 |
2 |
} |
364 |
|
|
365 |
|
public void printRaw(char[] buffer, int offset, int length) |
366 |
|
{ |
367 |
1 |
print(buffer, offset, length, true); |
368 |
1 |
} |
369 |
|
|
370 |
|
public void print(char[] buffer, int offset, int length, boolean raw) |
371 |
|
{ |
372 |
135 |
if (_openTag) |
373 |
42 |
closeTag(); |
374 |
|
|
375 |
135 |
maybePrintFiltered(buffer, offset, length, raw, false); |
376 |
135 |
} |
377 |
|
|
378 |
|
public void print(String value) |
379 |
|
{ |
380 |
43 |
print(value, false); |
381 |
43 |
} |
382 |
|
|
383 |
|
public void printRaw(String value) |
384 |
|
{ |
385 |
77 |
print(value, true); |
386 |
77 |
} |
387 |
|
|
388 |
|
public void print(String value, boolean raw) |
389 |
|
{ |
390 |
132 |
if (value == null || value.length() == 0) |
391 |
|
{ |
392 |
3 |
print(null, 0, 0, raw); |
393 |
3 |
return; |
394 |
|
} |
395 |
|
|
396 |
129 |
char[] buffer = value.toCharArray(); |
397 |
|
|
398 |
129 |
print(buffer, 0, buffer.length, raw); |
399 |
129 |
} |
400 |
|
|
401 |
|
public void print(char value) |
402 |
|
{ |
403 |
1 |
char[] data = new char[] |
404 |
|
{ value }; |
405 |
|
|
406 |
1 |
print(data, 0, 1); |
407 |
1 |
} |
408 |
|
|
409 |
|
public void print(int value) |
410 |
|
{ |
411 |
2 |
if (_openTag) |
412 |
1 |
closeTag(); |
413 |
|
|
414 |
2 |
_writer.print(value); |
415 |
2 |
} |
416 |
|
|
417 |
|
public void println() |
418 |
|
{ |
419 |
23 |
if (_openTag) |
420 |
2 |
closeTag(); |
421 |
|
|
422 |
23 |
_writer.println(); |
423 |
23 |
} |
424 |
|
|
425 |
|
public String getContentType() |
426 |
|
{ |
427 |
9 |
return _contentType; |
428 |
|
} |
429 |
|
|
430 |
|
private void checkTagOpen() |
431 |
|
{ |
432 |
222 |
if (!_openTag) |
433 |
3 |
throw new IllegalStateException(MarkupMessages.tagNotOpen()); |
434 |
219 |
} |
435 |
|
|
436 |
|
private void push(String name) |
437 |
|
{ |
438 |
103 |
if (_activeElementStack == null) |
439 |
73 |
_activeElementStack = new ArrayList(); |
440 |
|
|
441 |
103 |
_activeElementStack.add(name); |
442 |
103 |
} |
443 |
|
|
444 |
|
private String pop() |
445 |
|
{ |
446 |
93 |
int lastIndex = _activeElementStack.size() - 1; |
447 |
|
|
448 |
93 |
return (String) _activeElementStack.remove(lastIndex); |
449 |
|
} |
450 |
|
|
451 |
|
private boolean stackEmpty() |
452 |
|
{ |
453 |
95 |
return _activeElementStack == null || _activeElementStack.isEmpty(); |
454 |
|
} |
455 |
|
} |