jjg@1736: /* jjg@1736: * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. jjg@1736: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@1736: * jjg@1736: * This code is free software; you can redistribute it and/or modify it jjg@1736: * under the terms of the GNU General Public License version 2 only, as jjg@1736: * published by the Free Software Foundation. Oracle designates this jjg@1736: * particular file as subject to the "Classpath" exception as provided jjg@1736: * by Oracle in the LICENSE file that accompanied this code. jjg@1736: * jjg@1736: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@1736: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@1736: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@1736: * version 2 for more details (a copy is included in the LICENSE file that jjg@1736: * accompanied this code). jjg@1736: * jjg@1736: * You should have received a copy of the GNU General Public License version jjg@1736: * 2 along with this work; if not, write to the Free Software Foundation, jjg@1736: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@1736: * jjg@1736: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jjg@1736: * or visit www.oracle.com if you need additional information or have any jjg@1736: * questions. jjg@1736: */ jjg@1736: jjg@1736: package com.sun.tools.doclets.formats.html.markup; jjg@1736: jjg@1736: import java.io.IOException; jjg@1736: import java.io.Writer; jjg@1736: import java.util.ArrayList; jjg@1736: import java.util.Collections; jjg@1736: import java.util.List; jjg@1736: import com.sun.tools.doclets.internal.toolkit.Content; jjg@1736: jjg@1736: /** jjg@1736: * A sequence of Content nodes. jjg@1736: */ jjg@1736: public class ContentBuilder extends Content { jjg@1736: protected List contents = Collections.emptyList(); jjg@1736: jjg@1736: @Override jjg@1736: public void addContent(Content content) { jjg@1736: if (content.isEmpty()) jjg@1736: return; jjg@1736: ensureMutableContents(); jjg@1736: if (content instanceof ContentBuilder) { jjg@1736: contents.addAll(((ContentBuilder) content).contents); jjg@1736: } else jjg@1736: contents.add(content); jjg@1736: } jjg@1736: jjg@1736: @Override jjg@1736: public void addContent(String text) { jjg@1736: if (text.isEmpty()) jjg@1736: return; jjg@1736: ensureMutableContents(); jjg@1736: Content c = contents.isEmpty() ? null : contents.get(contents.size() - 1); jjg@1736: StringContent sc; jjg@1736: if (c != null && c instanceof StringContent) { jjg@1736: sc = (StringContent) c; jjg@1736: } else { jjg@1736: contents.add(sc = new StringContent()); jjg@1736: } jjg@1736: sc.addContent(text); jjg@1736: } jjg@1736: jjg@1736: @Override jjg@1736: public boolean write(Writer writer, boolean atNewline) throws IOException { jjg@1736: for (Content content: contents) { jjg@1736: atNewline = content.write(writer, atNewline); jjg@1736: } jjg@1736: return atNewline; jjg@1736: } jjg@1736: jjg@1736: @Override jjg@1736: public boolean isEmpty() { jjg@1736: for (Content content: contents) { jjg@1736: if (!content.isEmpty()) jjg@1736: return false; jjg@1736: } jjg@1736: return true; jjg@1736: } jjg@1736: jjg@1736: private void ensureMutableContents() { jjg@1736: if (contents.isEmpty()) jjg@1736: contents = new ArrayList(); jjg@1736: } jjg@1736: }