|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| package org.apache.tapestry.markup; |
|
16 |
| |
|
17 |
| import java.io.PrintWriter; |
|
18 |
| import java.util.ArrayList; |
|
19 |
| import java.util.List; |
|
20 |
| |
|
21 |
| import org.apache.hivemind.ApplicationRuntimeException; |
|
22 |
| import org.apache.hivemind.util.Defense; |
|
23 |
| import org.apache.tapestry.IMarkupWriter; |
|
24 |
| import org.apache.tapestry.NestedMarkupWriter; |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| public class MarkupWriterImpl implements IMarkupWriter |
|
35 |
| { |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| private PrintWriter _writer; |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| private MarkupFilter _filter; |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| private boolean _openTag = false; |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| private boolean _emptyTag = false; |
|
64 |
| |
|
65 |
| private String _contentType; |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| private List _activeElementStack; |
|
74 |
| |
|
75 |
1158
| public MarkupWriterImpl(String contentType, PrintWriter writer, MarkupFilter filter)
|
|
76 |
| { |
|
77 |
1158
| Defense.notNull(contentType, "contentType");
|
|
78 |
1158
| Defense.notNull(writer, "writer");
|
|
79 |
1158
| Defense.notNull(filter, "filter");
|
|
80 |
| |
|
81 |
1158
| _contentType = contentType;
|
|
82 |
1158
| _writer = writer;
|
|
83 |
1158
| _filter = filter;
|
|
84 |
| } |
|
85 |
| |
|
86 |
459
| public void attribute(String name, int value)
|
|
87 |
| { |
|
88 |
459
| checkTagOpen();
|
|
89 |
| |
|
90 |
456
| _writer.print(' ');
|
|
91 |
456
| _writer.print(name);
|
|
92 |
456
| _writer.print("=\"");
|
|
93 |
456
| _writer.print(value);
|
|
94 |
456
| _writer.print('"');
|
|
95 |
| } |
|
96 |
| |
|
97 |
15
| public void attribute(String name, boolean value)
|
|
98 |
| { |
|
99 |
15
| checkTagOpen();
|
|
100 |
| |
|
101 |
12
| _writer.print(' ');
|
|
102 |
12
| _writer.print(name);
|
|
103 |
12
| _writer.print("=\"");
|
|
104 |
12
| _writer.print(value);
|
|
105 |
12
| _writer.print('"');
|
|
106 |
| } |
|
107 |
| |
|
108 |
19569
| public void attribute(String name, String value)
|
|
109 |
| { |
|
110 |
19569
| attribute(name, value, false);
|
|
111 |
| } |
|
112 |
| |
|
113 |
19590
| public void attribute(String name, String value, boolean raw)
|
|
114 |
| { |
|
115 |
19590
| checkTagOpen();
|
|
116 |
| |
|
117 |
19587
| _writer.print(' ');
|
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
19587
| _writer.print(name);
|
|
122 |
19587
| _writer.print("=\"");
|
|
123 |
| |
|
124 |
19587
| if (value != null)
|
|
125 |
| { |
|
126 |
19551
| char[] data = value.toCharArray();
|
|
127 |
19551
| maybePrintFiltered(data, 0, data.length, raw, true);
|
|
128 |
| } |
|
129 |
| |
|
130 |
19587
| _writer.print('"');
|
|
131 |
| } |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
50790
| private void maybePrintFiltered(char[] data, int offset, int length, boolean raw,
|
|
138 |
| boolean isAttribute) |
|
139 |
| { |
|
140 |
50790
| if (data == null || length <= 0)
|
|
141 |
504
| return;
|
|
142 |
| |
|
143 |
50286
| if (raw)
|
|
144 |
| { |
|
145 |
9459
| _writer.write(data, offset, length);
|
|
146 |
9459
| return;
|
|
147 |
| } |
|
148 |
| |
|
149 |
40827
| _filter.print(_writer, data, offset, length, isAttribute);
|
|
150 |
| } |
|
151 |
| |
|
152 |
21
| public void attributeRaw(String name, String value)
|
|
153 |
| { |
|
154 |
21
| attribute(name, value, true);
|
|
155 |
| } |
|
156 |
| |
|
157 |
39996
| public void begin(String name)
|
|
158 |
| { |
|
159 |
39996
| if (_openTag)
|
|
160 |
11406
| closeTag();
|
|
161 |
| |
|
162 |
39996
| push(name);
|
|
163 |
| |
|
164 |
39996
| _writer.print('<');
|
|
165 |
39996
| _writer.print(name);
|
|
166 |
| |
|
167 |
39996
| _openTag = true;
|
|
168 |
39996
| _emptyTag = false;
|
|
169 |
| } |
|
170 |
| |
|
171 |
2346
| public void beginEmpty(String name)
|
|
172 |
| { |
|
173 |
2346
| if (_openTag)
|
|
174 |
135
| closeTag();
|
|
175 |
| |
|
176 |
2346
| _writer.print('<');
|
|
177 |
2346
| _writer.print(name);
|
|
178 |
| |
|
179 |
2346
| _openTag = true;
|
|
180 |
2346
| _emptyTag = true;
|
|
181 |
| } |
|
182 |
| |
|
183 |
0
| public boolean checkError()
|
|
184 |
| { |
|
185 |
0
| return _writer.checkError();
|
|
186 |
| } |
|
187 |
| |
|
188 |
909
| public void close()
|
|
189 |
| { |
|
190 |
909
| if (_openTag)
|
|
191 |
0
| closeTag();
|
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
909
| while (!stackEmpty())
|
|
196 |
| { |
|
197 |
9
| _writer.print("</");
|
|
198 |
9
| _writer.print(pop());
|
|
199 |
9
| _writer.print('>');
|
|
200 |
| } |
|
201 |
| |
|
202 |
909
| _writer.close();
|
|
203 |
| |
|
204 |
909
| _writer = null;
|
|
205 |
909
| _filter = null;
|
|
206 |
909
| _activeElementStack = null;
|
|
207 |
| } |
|
208 |
| |
|
209 |
42327
| public void closeTag()
|
|
210 |
| { |
|
211 |
42327
| if (_emptyTag)
|
|
212 |
2346
| _writer.print('/');
|
|
213 |
| |
|
214 |
42327
| _writer.print('>');
|
|
215 |
| |
|
216 |
42327
| _openTag = false;
|
|
217 |
42327
| _emptyTag = false;
|
|
218 |
| } |
|
219 |
| |
|
220 |
1263
| public void comment(String value)
|
|
221 |
| { |
|
222 |
1263
| if (_openTag)
|
|
223 |
3
| closeTag();
|
|
224 |
| |
|
225 |
1263
| _writer.print("<!-- ");
|
|
226 |
1263
| _writer.print(value);
|
|
227 |
1263
| _writer.println(" -->");
|
|
228 |
| } |
|
229 |
| |
|
230 |
16848
| public void end()
|
|
231 |
| { |
|
232 |
16848
| if (_openTag)
|
|
233 |
5190
| closeTag();
|
|
234 |
| |
|
235 |
16848
| if (stackEmpty())
|
|
236 |
3
| throw new ApplicationRuntimeException(MarkupMessages.endWithEmptyStack());
|
|
237 |
| |
|
238 |
16845
| _writer.print("</");
|
|
239 |
16845
| _writer.print(pop());
|
|
240 |
16845
| _writer.print('>');
|
|
241 |
| } |
|
242 |
| |
|
243 |
11844
| public void end(String name)
|
|
244 |
| { |
|
245 |
11844
| if (_openTag)
|
|
246 |
24
| closeTag();
|
|
247 |
| |
|
248 |
11844
| if (_activeElementStack == null || !_activeElementStack.contains(name))
|
|
249 |
3
| throw new ApplicationRuntimeException(MarkupMessages.elementNotOnStack(
|
|
250 |
| name, |
|
251 |
| _activeElementStack)); |
|
252 |
| |
|
253 |
11841
| while (true)
|
|
254 |
| { |
|
255 |
23079
| String tagName = pop();
|
|
256 |
| |
|
257 |
23079
| _writer.print("</");
|
|
258 |
23079
| _writer.print(tagName);
|
|
259 |
23079
| _writer.print('>');
|
|
260 |
| |
|
261 |
23079
| if (tagName.equals(name))
|
|
262 |
11841
| break;
|
|
263 |
| } |
|
264 |
| } |
|
265 |
| |
|
266 |
0
| public void flush()
|
|
267 |
| { |
|
268 |
0
| _writer.flush();
|
|
269 |
| } |
|
270 |
| |
|
271 |
576
| public NestedMarkupWriter getNestedWriter()
|
|
272 |
| { |
|
273 |
576
| return new NestedMarkupWriterImpl(this, _filter);
|
|
274 |
| } |
|
275 |
| |
|
276 |
6
| public void print(char[] data, int offset, int length)
|
|
277 |
| { |
|
278 |
6
| print(data, offset, length, false);
|
|
279 |
| } |
|
280 |
| |
|
281 |
8298
| public void printRaw(char[] buffer, int offset, int length)
|
|
282 |
| { |
|
283 |
8298
| print(buffer, offset, length, true);
|
|
284 |
| } |
|
285 |
| |
|
286 |
31239
| public void print(char[] buffer, int offset, int length, boolean raw)
|
|
287 |
| { |
|
288 |
31239
| if (_openTag)
|
|
289 |
21495
| closeTag();
|
|
290 |
| |
|
291 |
31239
| maybePrintFiltered(buffer, offset, length, raw, false);
|
|
292 |
| } |
|
293 |
| |
|
294 |
17802
| public void print(String value)
|
|
295 |
| { |
|
296 |
17802
| print(value, false);
|
|
297 |
| } |
|
298 |
| |
|
299 |
1152
| public void printRaw(String value)
|
|
300 |
| { |
|
301 |
1152
| print(value, true);
|
|
302 |
| } |
|
303 |
| |
|
304 |
22935
| public void print(String value, boolean raw)
|
|
305 |
| { |
|
306 |
22935
| if (value == null || value.length() == 0)
|
|
307 |
| { |
|
308 |
261
| print(null, 0, 0, raw);
|
|
309 |
261
| return;
|
|
310 |
| } |
|
311 |
| |
|
312 |
22674
| char[] buffer = value.toCharArray();
|
|
313 |
| |
|
314 |
22674
| print(buffer, 0, buffer.length, raw);
|
|
315 |
| } |
|
316 |
| |
|
317 |
3
| public void print(char value)
|
|
318 |
| { |
|
319 |
3
| char[] data = new char[]
|
|
320 |
| { value }; |
|
321 |
| |
|
322 |
3
| print(data, 0, 1);
|
|
323 |
| } |
|
324 |
| |
|
325 |
426
| public void print(int value)
|
|
326 |
| { |
|
327 |
426
| if (_openTag)
|
|
328 |
423
| closeTag();
|
|
329 |
| |
|
330 |
426
| _writer.print(value);
|
|
331 |
| } |
|
332 |
| |
|
333 |
20919
| public void println()
|
|
334 |
| { |
|
335 |
20919
| if (_openTag)
|
|
336 |
3504
| closeTag();
|
|
337 |
| |
|
338 |
20919
| _writer.println();
|
|
339 |
| } |
|
340 |
| |
|
341 |
897
| public String getContentType()
|
|
342 |
| { |
|
343 |
897
| return _contentType;
|
|
344 |
| } |
|
345 |
| |
|
346 |
20064
| private void checkTagOpen()
|
|
347 |
| { |
|
348 |
20064
| if (!_openTag)
|
|
349 |
9
| throw new IllegalStateException(MarkupMessages.tagNotOpen());
|
|
350 |
| } |
|
351 |
| |
|
352 |
39996
| private void push(String name)
|
|
353 |
| { |
|
354 |
39996
| if (_activeElementStack == null)
|
|
355 |
705
| _activeElementStack = new ArrayList();
|
|
356 |
| |
|
357 |
39996
| _activeElementStack.add(name);
|
|
358 |
| } |
|
359 |
| |
|
360 |
39933
| private String pop()
|
|
361 |
| { |
|
362 |
39933
| int lastIndex = _activeElementStack.size() - 1;
|
|
363 |
| |
|
364 |
39933
| return (String) _activeElementStack.remove(lastIndex);
|
|
365 |
| } |
|
366 |
| |
|
367 |
17766
| private boolean stackEmpty()
|
|
368 |
| { |
|
369 |
17766
| return _activeElementStack == null || _activeElementStack.isEmpty();
|
|
370 |
| } |
|
371 |
| } |