Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
NestedMarkupWriterImpl |
|
| 1.5;1.5 |
1 | // Copyright 2005 The Apache Software Foundation | |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | // you may not use this file except in compliance with the License. | |
5 | // You may obtain a copy of the License at | |
6 | // | |
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
8 | // | |
9 | // Unless required by applicable law or agreed to in writing, software | |
10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | // See the License for the specific language governing permissions and | |
13 | // limitations under the License. | |
14 | ||
15 | package org.apache.tapestry.markup; | |
16 | ||
17 | import java.io.CharArrayWriter; | |
18 | import java.io.PrintWriter; | |
19 | ||
20 | import org.apache.tapestry.IMarkupWriter; | |
21 | import org.apache.tapestry.NestedMarkupWriter; | |
22 | ||
23 | /** | |
24 | * Nested implementation of {@link org.apache.tapestry.IMarkupWriter}. Accumulates content in a | |
25 | * {@link java.io.CharArrayWriter}, and prints the buffered content (raw) on {@link #close()}. | |
26 | * | |
27 | * @author Howard M. Lewis Ship | |
28 | * @since 4.0 | |
29 | * @see org.apache.tapestry.IMarkupWriter#getNestedWriter() | |
30 | */ | |
31 | public class NestedMarkupWriterImpl extends MarkupWriterImpl implements NestedMarkupWriter | |
32 | { | |
33 | private final IMarkupWriter _parent; | |
34 | ||
35 | private final CharArrayWriter _charArrayWriter; | |
36 | ||
37 | private boolean _closed; | |
38 | ||
39 | public NestedMarkupWriterImpl(IMarkupWriter parent, MarkupFilter filter) | |
40 | { | |
41 | // Need to do this awkward double constructor because we want | |
42 | // to create an object and pass it to the parent constructor. | |
43 | // Java language rules get in the way here. | |
44 | ||
45 | 0 | this(parent, new CharArrayWriter(), filter); |
46 | 0 | } |
47 | ||
48 | private NestedMarkupWriterImpl(IMarkupWriter parent, CharArrayWriter writer, MarkupFilter filter) | |
49 | { | |
50 | 0 | super(parent.getContentType(), new PrintWriter(writer), filter); |
51 | ||
52 | 0 | _parent = parent; |
53 | 0 | _charArrayWriter = writer; |
54 | 0 | } |
55 | ||
56 | public String getBuffer() | |
57 | { | |
58 | 0 | if (_closed) |
59 | 0 | throw new IllegalStateException(MarkupMessages.closeOnce()); |
60 | ||
61 | 0 | _closed = true; |
62 | ||
63 | 0 | super.close(); |
64 | ||
65 | 0 | return _charArrayWriter.toString(); |
66 | } | |
67 | ||
68 | /** | |
69 | * Closes the internal {@link CharArrayWriter}, then captures its content and invokes | |
70 | * {@link org.apache.tapestry.IMarkupWriter#printRaw(String)} on the parent markup writer | |
71 | * (the writer that created this writer). | |
72 | */ | |
73 | ||
74 | public void close() | |
75 | { | |
76 | 0 | String content = getBuffer(); |
77 | ||
78 | 0 | _parent.printRaw(content); |
79 | 0 | } |
80 | } |