001: /*
002: * Copyright 2003-2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.doclets.formats.html;
027:
028: import com.sun.tools.doclets.internal.toolkit.util.links.*;
029: import com.sun.javadoc.*;
030: import com.sun.tools.doclets.internal.toolkit.*;
031: import com.sun.tools.doclets.internal.toolkit.util.*;
032:
033: /**
034: * A factory that returns a link given the information about it.
035: *
036: * @author Jamie Ho
037: * @since 1.5
038: */
039: public class LinkFactoryImpl extends LinkFactory {
040:
041: private HtmlDocletWriter m_writer;
042:
043: public LinkFactoryImpl(HtmlDocletWriter writer) {
044: m_writer = writer;
045: }
046:
047: /**
048: * {@inheritDoc}
049: */
050: protected LinkOutput getOutputInstance() {
051: return new LinkOutputImpl();
052: }
053:
054: /**
055: * {@inheritDoc}
056: */
057: protected LinkOutput getClassLink(LinkInfo linkInfo) {
058: LinkInfoImpl classLinkInfo = (LinkInfoImpl) linkInfo;
059: boolean noLabel = linkInfo.label == null
060: || linkInfo.label.length() == 0;
061: ClassDoc classDoc = classLinkInfo.classDoc;
062: //Create a tool tip if we are linking to a class or interface. Don't
063: //create one if we are linking to a member.
064: String title = (classLinkInfo.where == null || classLinkInfo.where
065: .length() == 0) ? getClassToolTip(classDoc,
066: classLinkInfo.type != null
067: && !classDoc.qualifiedTypeName().equals(
068: classLinkInfo.type.qualifiedTypeName()))
069: : "";
070: StringBuffer label = new StringBuffer(classLinkInfo
071: .getClassLinkLabel(m_writer.configuration));
072: classLinkInfo.displayLength += label.length();
073: if (noLabel && classLinkInfo.excludeTypeParameterLinks) {
074: label.append(getTypeParameterLinks(linkInfo).toString());
075: }
076: Configuration configuration = ConfigurationImpl.getInstance();
077: LinkOutputImpl linkOutput = new LinkOutputImpl();
078: if (classDoc.isIncluded()) {
079: if (configuration.isGeneratedDoc(classDoc)) {
080: String filename = pathString(classLinkInfo);
081: if (linkInfo.linkToSelf
082: || !(linkInfo.classDoc.name() + ".html")
083: .equals(m_writer.filename)) {
084: linkOutput.append(m_writer.getHyperLink(filename,
085: classLinkInfo.where, label.toString(),
086: classLinkInfo.isBold,
087: classLinkInfo.styleName, title,
088: classLinkInfo.target));
089: if (noLabel
090: && !classLinkInfo.excludeTypeParameterLinks) {
091: linkOutput.append(getTypeParameterLinks(
092: linkInfo).toString());
093: }
094: return linkOutput;
095: }
096: }
097: } else {
098: String crossLink = m_writer.getCrossClassLink(classDoc
099: .qualifiedName(), classLinkInfo.where, label
100: .toString(), classLinkInfo.isBold,
101: classLinkInfo.styleName, true);
102: if (crossLink != null) {
103: linkOutput.append(crossLink);
104: if (noLabel && !classLinkInfo.excludeTypeParameterLinks) {
105: linkOutput.append(getTypeParameterLinks(linkInfo)
106: .toString());
107: }
108: return linkOutput;
109: }
110: }
111: // Can't link so just write label.
112: linkOutput.append(label.toString());
113: if (noLabel && !classLinkInfo.excludeTypeParameterLinks) {
114: linkOutput.append(getTypeParameterLinks(linkInfo)
115: .toString());
116: }
117: return linkOutput;
118: }
119:
120: /**
121: * {@inheritDoc}
122: */
123: protected LinkOutput getTypeParameterLink(LinkInfo linkInfo,
124: Type typeParam) {
125: LinkInfoImpl typeLinkInfo = new LinkInfoImpl(linkInfo
126: .getContext(), typeParam);
127: typeLinkInfo.excludeTypeBounds = linkInfo.excludeTypeBounds;
128: typeLinkInfo.excludeTypeParameterLinks = linkInfo.excludeTypeParameterLinks;
129: typeLinkInfo.linkToSelf = linkInfo.linkToSelf;
130: LinkOutput output = getLinkOutput(typeLinkInfo);
131: ((LinkInfoImpl) linkInfo).displayLength += typeLinkInfo.displayLength;
132: return output;
133: }
134:
135: /**
136: * Given a class, return the appropriate tool tip.
137: *
138: * @param classDoc the class to get the tool tip for.
139: * @return the tool tip for the appropriate class.
140: */
141: private String getClassToolTip(ClassDoc classDoc, boolean isTypeLink) {
142: Configuration configuration = ConfigurationImpl.getInstance();
143: if (isTypeLink) {
144: return configuration.getText(
145: "doclet.Href_Type_Param_Title", classDoc.name());
146: } else if (classDoc.isInterface()) {
147: return configuration.getText("doclet.Href_Interface_Title",
148: Util.getPackageName(classDoc.containingPackage()));
149: } else if (classDoc.isAnnotationType()) {
150: return configuration.getText(
151: "doclet.Href_Annotation_Title", Util
152: .getPackageName(classDoc
153: .containingPackage()));
154: } else if (classDoc.isEnum()) {
155: return configuration.getText("doclet.Href_Enum_Title", Util
156: .getPackageName(classDoc.containingPackage()));
157: } else {
158: return configuration.getText("doclet.Href_Class_Title",
159: Util.getPackageName(classDoc.containingPackage()));
160: }
161: }
162:
163: /**
164: * Return path to the given file name in the given package. So if the name
165: * passed is "Object.html" and the name of the package is "java.lang", and
166: * if the relative path is "../.." then returned string will be
167: * "../../java/lang/Object.html"
168: *
169: * @param linkInfo the information about the link.
170: * @param fileName the file name, to which path string is.
171: */
172: private String pathString(LinkInfoImpl linkInfo) {
173: if (linkInfo.context == LinkInfoImpl.PACKAGE_FRAME) {
174: //Not really necessary to do this but we want to be consistent
175: //with 1.4.2 output.
176: return linkInfo.classDoc.name() + ".html";
177: }
178: StringBuffer buf = new StringBuffer(m_writer.relativePath);
179: buf.append(DirectoryManager.getPathToPackage(linkInfo.classDoc
180: .containingPackage(), linkInfo.classDoc.name()
181: + ".html"));
182: return buf.toString();
183: }
184: }
|