langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/taglets/ValueTaglet.java
author jjg
Tue, 14 May 2013 10:14:57 -0700
changeset 17574 044c7e1e4d53
parent 17569 ef80738645e2
child 19667 fdfce85627a9
permissions -rw-r--r--
8012308: Remove TagletOutput in favor of direct use of Content Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
17569
ef80738645e2 8012178: Cleanup use of Util.escapeHtmlChars
jjg
parents: 14260
diff changeset
     2
 * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.doclets.internal.toolkit.taglets;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 5520
diff changeset
    28
import java.util.*;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 5520
diff changeset
    29
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.javadoc.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.doclets.internal.toolkit.Configuration;
17574
044c7e1e4d53 8012308: Remove TagletOutput in favor of direct use of Content
jjg
parents: 17569
diff changeset
    32
import com.sun.tools.doclets.internal.toolkit.Content;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.doclets.internal.toolkit.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 * An inline Taglet representing the value tag. This tag should only be used with
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 * constant fields that have a value.  It is used to access the value of constant
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 * fields.  This inline tag has an optional field name parameter.  If the name is
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 * specified, the constant value is retrieved from the specified field.  A link
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * is also created to the specified field.  If a name is not specified, the value
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 * is retrieved for the field that the inline tag appears on.  The name is specifed
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 * in the following format:  [fully qualified class name]#[constant field name].
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 *
14260
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14259
diff changeset
    44
 *  <p><b>This is NOT part of any supported API.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14259
diff changeset
    45
 *  If you write code that depends on this, you do so at your own risk.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14259
diff changeset
    46
 *  This code and its internal interfaces are subject to change or
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14259
diff changeset
    47
 *  deletion without notice.</b>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 * @author Jamie Ho
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 * @since 1.4
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
public class ValueTaglet extends BaseInlineTaglet {
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
     * Construct a new ValueTaglet.
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    public ValueTaglet() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        name = "value";
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     * Will return false because this inline tag may
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     * only appear in Fields.
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
     * @return false since this is not a method.
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    public boolean inMethod() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
     * Will return false because this inline tag may
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     * only appear in Fields.
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     * @return false since this is not a method.
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    public boolean inConstructor() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
     * Will return false because this inline tag may
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
     * only appear in Fields.
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
     * @return false since this is not a method.
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    public boolean inOverview() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
     * Will return false because this inline tag may
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
     * only appear in Fields.
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     * @return false since this is not a method.
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
    public boolean inPackage() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
     * Will return false because this inline tag may
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
     * only appear in Fields.
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
     * @return false since this is not a method.
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    public boolean inType() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
     * Given the name of the field, return the corresponding FieldDoc.
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
     * @param config the current configuration of the doclet.
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     * @param tag the value tag.
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     * @param name the name of the field to search for.  The name should be in
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 14258
diff changeset
   113
     * {@code <qualified class name>#<field name>} format. If the class name is omitted,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
     * it is assumed that the field is in the current class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
     * @return the corresponding FieldDoc. If the name is null or empty string,
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
     * return field that the value tag was used in.
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
     * @throws DocletAbortException if the value tag does not specify a name to
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
     * a value field and it is not used within the comments of a valid field.
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    private FieldDoc getFieldDoc(Configuration config, Tag tag, String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        if (name == null || name.length() == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
            //Base case: no label.
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
            if (tag.holder() instanceof FieldDoc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
                return (FieldDoc) tag.holder();
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
                //This should never ever happen.
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
                throw new DocletAbortException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        StringTokenizer st = new StringTokenizer(name, "#");
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
        String memberName = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        ClassDoc cd = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        if (st.countTokens() == 1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
            //Case 2:  @value in same class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
            Doc holder = tag.holder();
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
            if (holder instanceof MemberDoc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
                cd = ((MemberDoc) holder).containingClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
            } else if (holder instanceof ClassDoc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
                cd = (ClassDoc) holder;
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
            memberName = st.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            //Case 3: @value in different class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
            cd = config.root.classNamed(st.nextToken());
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
            memberName = st.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        if (cd == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        FieldDoc[] fields = cd.fields();
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        for (int i = 0; i < fields.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
            if (fields[i].name().equals(memberName)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
                return fields[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
     * {@inheritDoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
     */
17574
044c7e1e4d53 8012308: Remove TagletOutput in favor of direct use of Content
jjg
parents: 17569
diff changeset
   164
    public Content getTagletOutput(Tag tag, TagletWriter writer) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        FieldDoc field = getFieldDoc(
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
            writer.configuration(), tag, tag.text());
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        if (field == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
            //Reference is unknown.
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
            writer.getMsgRetriever().warning(tag.holder().position(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
                "doclet.value_tag_invalid_reference", tag.text());
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        } else if (field.constantValue() != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
            return writer.valueTagOutput(field,
17569
ef80738645e2 8012178: Cleanup use of Util.escapeHtmlChars
jjg
parents: 14260
diff changeset
   173
                field.constantValueExpression(),
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
                ! field.equals(tag.holder()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
            //Referenced field is not a constant.
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
            writer.getMsgRetriever().warning(tag.holder().position(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
                "doclet.value_tag_invalid_constant", field.name());
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        return writer.getOutputInstance();
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
}