KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > markup > NestedMarkupWriterImpl


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 JavaDoc;
18 import java.io.PrintWriter JavaDoc;
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 JavaDoc _charArrayWriter;
36
37     private boolean _closed;
38
39     public String JavaDoc getBuffer()
40     {
41         return _charArrayWriter.toString();
42     }
43
44     public NestedMarkupWriterImpl(IMarkupWriter parent, MarkupFilter filter)
45     {
46         // Need to do this awkward double constructor because we want
47
// to create an object and pass it to the parent constructor.
48
// Java language rules get in the way here.
49

50         this(parent, new CharArrayWriter JavaDoc(), filter);
51     }
52
53     private NestedMarkupWriterImpl(IMarkupWriter parent, CharArrayWriter JavaDoc writer, MarkupFilter filter)
54     {
55         super(parent.getContentType(), new PrintWriter JavaDoc(writer), filter);
56
57         _parent = parent;
58         _charArrayWriter = writer;
59     }
60
61     /**
62      * Closes the internal {@link CharArrayWriter}, then captures its content and invokes
63      * {@link org.apache.tapestry.IMarkupWriter#printRaw(String)} on the parent markup writer
64      * (the writer that created this writer).
65      */

66
67     public void close()
68     {
69         if (_closed)
70             throw new IllegalStateException JavaDoc(MarkupMessages.closeOnce());
71
72         _closed = true;
73
74         super.close();
75
76         String JavaDoc content = getBuffer();
77
78         _parent.printRaw(content);
79     }
80 }
Popular Tags