src/share/classes/com/sun/tools/doclets/internal/toolkit/taglets/ValueTaglet.java

Tue, 09 Oct 2012 19:10:00 -0700

author
jjg
date
Tue, 09 Oct 2012 19:10:00 -0700
changeset 1357
c75be5bc5283
parent 554
9d9f26857129
child 1358
fc123bdeddb8
permissions
-rw-r--r--

8000663: clean up langtools imports
Reviewed-by: darcy

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

mercurial