0N/A/*
553N/A * Copyright (c) 2001, 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
553N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
553N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.tools.doclets.internal.toolkit.taglets;
0N/A
0N/Aimport com.sun.javadoc.*;
0N/Aimport com.sun.tools.doclets.internal.toolkit.Configuration;
0N/Aimport com.sun.tools.doclets.internal.toolkit.util.*;
0N/Aimport java.util.*;
0N/A
0N/A/**
0N/A * An inline Taglet representing the value tag. This tag should only be used with
0N/A * constant fields that have a value. It is used to access the value of constant
0N/A * fields. This inline tag has an optional field name parameter. If the name is
0N/A * specified, the constant value is retrieved from the specified field. A link
0N/A * is also created to the specified field. If a name is not specified, the value
0N/A * is retrieved for the field that the inline tag appears on. The name is specifed
0N/A * in the following format: [fully qualified class name]#[constant field name].
0N/A *
0N/A * This code is not part of an API.
0N/A * It is implementation that is subject to change.
0N/A * Do not use it as an API
0N/A *
0N/A * @author Jamie Ho
0N/A * @since 1.4
0N/A */
0N/A
0N/Apublic class ValueTaglet extends BaseInlineTaglet {
0N/A
0N/A /**
0N/A * Construct a new ValueTaglet.
0N/A */
0N/A public ValueTaglet() {
0N/A name = "value";
0N/A }
0N/A
0N/A /**
0N/A * Will return false because this inline tag may
0N/A * only appear in Fields.
0N/A * @return false since this is not a method.
0N/A */
0N/A public boolean inMethod() {
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Will return false because this inline tag may
0N/A * only appear in Fields.
0N/A * @return false since this is not a method.
0N/A */
0N/A public boolean inConstructor() {
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Will return false because this inline tag may
0N/A * only appear in Fields.
0N/A * @return false since this is not a method.
0N/A */
0N/A public boolean inOverview() {
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Will return false because this inline tag may
0N/A * only appear in Fields.
0N/A * @return false since this is not a method.
0N/A */
0N/A public boolean inPackage() {
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Will return false because this inline tag may
0N/A * only appear in Fields.
0N/A * @return false since this is not a method.
0N/A */
0N/A public boolean inType() {
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Given the name of the field, return the corresponding FieldDoc.
0N/A *
0N/A * @param config the current configuration of the doclet.
0N/A * @param tag the value tag.
0N/A * @param name the name of the field to search for. The name should be in
0N/A * <qualified class name>#<field name> format. If the class name is omitted,
0N/A * it is assumed that the field is in the current class.
0N/A *
0N/A * @return the corresponding FieldDoc. If the name is null or empty string,
0N/A * return field that the value tag was used in.
0N/A *
0N/A * @throws DocletAbortException if the value tag does not specify a name to
0N/A * a value field and it is not used within the comments of a valid field.
0N/A */
0N/A private FieldDoc getFieldDoc(Configuration config, Tag tag, String name) {
0N/A if (name == null || name.length() == 0) {
0N/A //Base case: no label.
0N/A if (tag.holder() instanceof FieldDoc) {
0N/A return (FieldDoc) tag.holder();
0N/A } else {
0N/A //This should never ever happen.
0N/A throw new DocletAbortException();
0N/A }
0N/A }
0N/A StringTokenizer st = new StringTokenizer(name, "#");
0N/A String memberName = null;
0N/A ClassDoc cd = null;
0N/A if (st.countTokens() == 1) {
0N/A //Case 2: @value in same class.
0N/A Doc holder = tag.holder();
0N/A if (holder instanceof MemberDoc) {
0N/A cd = ((MemberDoc) holder).containingClass();
0N/A } else if (holder instanceof ClassDoc) {
0N/A cd = (ClassDoc) holder;
0N/A }
0N/A memberName = st.nextToken();
0N/A } else {
0N/A //Case 3: @value in different class.
0N/A cd = config.root.classNamed(st.nextToken());
0N/A memberName = st.nextToken();
0N/A }
0N/A if (cd == null) {
0N/A return null;
0N/A }
0N/A FieldDoc[] fields = cd.fields();
0N/A for (int i = 0; i < fields.length; i++) {
0N/A if (fields[i].name().equals(memberName)) {
0N/A return fields[i];
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A */
0N/A public TagletOutput getTagletOutput(Tag tag, TagletWriter writer) {
0N/A FieldDoc field = getFieldDoc(
0N/A writer.configuration(), tag, tag.text());
0N/A if (field == null) {
0N/A //Reference is unknown.
0N/A writer.getMsgRetriever().warning(tag.holder().position(),
0N/A "doclet.value_tag_invalid_reference", tag.text());
0N/A } else if (field.constantValue() != null) {
0N/A return writer.valueTagOutput(field,
0N/A Util.escapeHtmlChars(field.constantValueExpression()),
0N/A ! field.equals(tag.holder()));
0N/A } else {
0N/A //Referenced field is not a constant.
0N/A writer.getMsgRetriever().warning(tag.holder().position(),
0N/A "doclet.value_tag_invalid_constant", field.name());
0N/A }
0N/A return writer.getOutputInstance();
0N/A }
0N/A}