|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| NestedMarkupWriterImpl.java | 100% | 100% | 100% | 100% |
|
||||||||||||||
| 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 | 543 | public String getBuffer() |
| 40 | { | |
| 41 | 543 | if (_closed) |
| 42 | 3 | throw new IllegalStateException(MarkupMessages.closeOnce()); |
| 43 | ||
| 44 | 540 | _closed = true; |
| 45 | ||
| 46 | 540 | super.close(); |
| 47 | ||
| 48 | 540 | return _charArrayWriter.toString(); |
| 49 | } | |
| 50 | ||
| 51 | 576 | public NestedMarkupWriterImpl(IMarkupWriter parent, MarkupFilter filter) |
| 52 | { | |
| 53 | // Need to do this awkward double constructor because we want | |
| 54 | // to create an object and pass it to the parent constructor. | |
| 55 | // Java language rules get in the way here. | |
| 56 | ||
| 57 | 576 | this(parent, new CharArrayWriter(), filter); |
| 58 | } | |
| 59 | ||
| 60 | 576 | private NestedMarkupWriterImpl(IMarkupWriter parent, CharArrayWriter writer, MarkupFilter filter) |
| 61 | { | |
| 62 | 576 | super(parent.getContentType(), new PrintWriter(writer), filter); |
| 63 | ||
| 64 | 576 | _parent = parent; |
| 65 | 576 | _charArrayWriter = writer; |
| 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 | 543 | public void close() |
| 75 | { | |
| 76 | 543 | String content = getBuffer(); |
| 77 | ||
| 78 | 540 | _parent.printRaw(content); |
| 79 | } | |
| 80 | } |
|
||||||||||