001/*
002 * Copyright (c) 2003, 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;
030import java.util.ArrayList;
031import java.util.List;
032import java.util.Objects;
033
034import org.jdrupes.mdoclet.internal.doclets.toolkit.Content;
035
036/**
037 * A sequence of Content nodes.
038 */
039public class ContentBuilder extends Content {
040    protected List<Content> contents = List.of();
041
042    public ContentBuilder() {
043    }
044
045    public ContentBuilder(Content... contents) {
046        for (Content c : contents) {
047            add(c);
048        }
049    }
050
051    @Override
052    public ContentBuilder add(Content content) {
053        Objects.requireNonNull(content);
054        ensureMutableContents();
055        if (content instanceof ContentBuilder cb) {
056            contents.addAll(cb.contents);
057        } else {
058            contents.add(content);
059        }
060        return this;
061    }
062
063    @Override
064    public ContentBuilder add(CharSequence text) {
065        if (text.length() > 0) {
066            ensureMutableContents();
067            Content c
068                = contents.isEmpty() ? null : contents.get(contents.size() - 1);
069            TextBuilder tb;
070            if (c instanceof TextBuilder tbi) {
071                tb = tbi;
072            } else {
073                contents.add(tb = new TextBuilder());
074            }
075            tb.add(text);
076        }
077        return this;
078    }
079
080    @Override
081    public boolean write(Writer writer, String newline, boolean atNewline)
082            throws IOException {
083        for (Content content : contents) {
084            atNewline = content.write(writer, newline, atNewline);
085        }
086        return atNewline;
087    }
088
089    @Override
090    public boolean isEmpty() {
091        for (Content content : contents) {
092            if (!content.isEmpty())
093                return false;
094        }
095        return true;
096    }
097
098    @Override
099    public int charCount() {
100        int n = 0;
101        for (Content c : contents)
102            n += c.charCount();
103        return n;
104    }
105
106    private void ensureMutableContents() {
107        if (contents.isEmpty())
108            contents = new ArrayList<>();
109    }
110}