001// Copyright 2006, 2008, 2009 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.dom;
016
017import java.io.PrintWriter;
018import java.util.Map;
019
020/**
021 * A type of node that contains text.
022 */
023public final class Text extends Node
024{
025    private final StringBuilder buffer;
026
027    Text(Element container, String text)
028    {
029        super(container);
030
031        buffer = new StringBuilder(text.length());
032
033        write(text);
034    }
035
036    boolean isEmpty()
037    {
038        return buffer.length() == 0 || buffer.toString().trim().length() == 0;
039    }
040
041    /**
042     * Writes additional text into the node, appending it to any existing text.
043     */
044    public void write(String text)
045    {
046        buffer.append(text);
047    }
048
049    public void writef(String format, Object... args)
050    {
051        write(String.format(format, args));
052    }
053
054    @Override
055    void toMarkup(Document document, PrintWriter writer, Map<String, String> namespaceURIToPrefix)
056    {
057        String encoded = document.getMarkupModel().encode(buffer.toString());
058
059        writer.print(encoded);
060    }
061}