langtools/src/share/classes/com/sun/tools/doclets/formats/html/markup/ContentBuilder.java
author briangoetz
Wed, 18 Dec 2013 16:05:18 -0500
changeset 22163 3651128c74eb
parent 21023 1192bdc59d82
permissions -rw-r--r--
8030244: Update langtools to use Diamond Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17559
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
     1
/*
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
     2
 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
     4
 *
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    10
 *
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    15
 * accompanied this code).
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    16
 *
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    20
 *
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    23
 * questions.
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    24
 */
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    25
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    26
package com.sun.tools.doclets.formats.html.markup;
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    27
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    28
import java.io.IOException;
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    29
import java.io.Writer;
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    30
import java.util.ArrayList;
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    31
import java.util.Collections;
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    32
import java.util.List;
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    33
import com.sun.tools.doclets.internal.toolkit.Content;
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    34
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    35
/**
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    36
 * A sequence of Content nodes.
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    37
 */
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    38
public class ContentBuilder extends Content {
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    39
    protected List<Content> contents = Collections.<Content>emptyList();
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    40
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    41
    @Override
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    42
    public void addContent(Content content) {
17574
044c7e1e4d53 8012308: Remove TagletOutput in favor of direct use of Content
jjg
parents: 17563
diff changeset
    43
        nullCheck(content);
17559
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    44
        ensureMutableContents();
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    45
        if (content instanceof ContentBuilder) {
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    46
            contents.addAll(((ContentBuilder) content).contents);
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    47
        } else
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    48
            contents.add(content);
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    49
    }
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    50
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    51
    @Override
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    52
    public void addContent(String text) {
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    53
        if (text.isEmpty())
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    54
            return;
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    55
        ensureMutableContents();
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    56
        Content c = contents.isEmpty() ? null : contents.get(contents.size() - 1);
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    57
        StringContent sc;
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    58
        if (c != null && c instanceof StringContent) {
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    59
            sc = (StringContent) c;
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    60
        } else {
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    61
            contents.add(sc = new StringContent());
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    62
        }
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    63
        sc.addContent(text);
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    64
    }
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    65
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    66
    @Override
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    67
    public boolean write(Writer writer, boolean atNewline) throws IOException {
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    68
        for (Content content: contents) {
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    69
            atNewline = content.write(writer, atNewline);
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    70
        }
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    71
        return atNewline;
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    72
    }
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    73
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    74
    @Override
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    75
    public boolean isEmpty() {
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    76
        for (Content content: contents) {
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    77
            if (!content.isEmpty())
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    78
                return false;
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    79
        }
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    80
        return true;
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    81
    }
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    82
17563
e8ddeb9bd17c 8011668: Allow HTMLWriter.getResource to take Content args
jjg
parents: 17562
diff changeset
    83
    @Override
17560
9f6771abbd1a 8011650: reduce use of RawHtml nodes in doclet
jjg
parents: 17559
diff changeset
    84
    public int charCount() {
9f6771abbd1a 8011650: reduce use of RawHtml nodes in doclet
jjg
parents: 17559
diff changeset
    85
        int n = 0;
9f6771abbd1a 8011650: reduce use of RawHtml nodes in doclet
jjg
parents: 17559
diff changeset
    86
        for (Content c : contents)
9f6771abbd1a 8011650: reduce use of RawHtml nodes in doclet
jjg
parents: 17559
diff changeset
    87
            n += c.charCount();
9f6771abbd1a 8011650: reduce use of RawHtml nodes in doclet
jjg
parents: 17559
diff changeset
    88
        return n;
9f6771abbd1a 8011650: reduce use of RawHtml nodes in doclet
jjg
parents: 17559
diff changeset
    89
    }
9f6771abbd1a 8011650: reduce use of RawHtml nodes in doclet
jjg
parents: 17559
diff changeset
    90
17559
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    91
    private void ensureMutableContents() {
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    92
        if (contents.isEmpty())
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21023
diff changeset
    93
            contents = new ArrayList<>();
17559
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    94
    }
87c28ee29329 8011642: Remove LinkOutput in favor of direct use of Content
jjg
parents:
diff changeset
    95
}