001/*
002 * Copyright (c) 2010, 2022, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package org.jdrupes.mdoclet.internal.doclets.formats.html.markup;
027
028import java.io.IOException;
029import java.io.Writer;
030
031import org.jdrupes.mdoclet.internal.doclets.toolkit.Content;
032
033/**
034 * Class for generating string content for HTML tags of javadoc output.
035 * The content is mutable to the extent that additional content may be added.
036 * Newlines are always represented by {@code \n}.
037 * Any special HTML characters will be escaped if and when the content is written out.
038 */
039public class TextBuilder extends Content {
040
041    private final StringBuilder stringBuilder;
042
043    /**
044     * Constructor to construct StringContent object.
045     */
046    public TextBuilder() {
047        stringBuilder = new StringBuilder();
048    }
049
050    /**
051     * Constructor to construct StringContent object with some initial content.
052     *
053     * @param initialContent initial content for the object
054     */
055    public TextBuilder(CharSequence initialContent) {
056        assert Text.checkNewlines(initialContent);
057        stringBuilder = new StringBuilder(initialContent);
058    }
059
060    /**
061     * Adds content for the StringContent object.
062     *
063     * @param strContent string content to be added
064     */
065    @Override
066    public TextBuilder add(CharSequence strContent) {
067        assert Text.checkNewlines(strContent);
068        stringBuilder.append(strContent);
069        return this;
070    }
071
072    @Override
073    public boolean isEmpty() {
074        return (stringBuilder.length() == 0);
075    }
076
077    @Override
078    public int charCount() {
079        return stringBuilder.length();
080    }
081
082    @Override
083    public String toString() {
084        return stringBuilder.toString();
085    }
086
087    @Override
088    public boolean write(Writer out, String newline, boolean atNewline)
089            throws IOException {
090        String s = Entity.escapeHtmlChars(stringBuilder);
091        out.write(s.replace("\n", newline));
092        return s.endsWith("\n");
093    }
094}