Tue, 14 May 2013 10:14:53 -0700
8011668: Allow HTMLWriter.getResource to take Content args
Reviewed-by: darcy
duke@1 | 1 | /* |
jjg@1521 | 2 | * Copyright (c) 2003, 2013, 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.formats.html; |
duke@1 | 27 | |
jjg@1521 | 28 | import java.util.List; |
jjg@1521 | 29 | |
duke@1 | 30 | import com.sun.javadoc.*; |
jjg@1736 | 31 | import com.sun.tools.doclets.formats.html.markup.ContentBuilder; |
jjg@1736 | 32 | import com.sun.tools.doclets.formats.html.markup.RawHtml; |
jjg@1737 | 33 | import com.sun.tools.doclets.formats.html.markup.StringContent; |
duke@1 | 34 | import com.sun.tools.doclets.internal.toolkit.*; |
duke@1 | 35 | import com.sun.tools.doclets.internal.toolkit.util.*; |
jjg@1357 | 36 | import com.sun.tools.doclets.internal.toolkit.util.links.*; |
duke@1 | 37 | |
duke@1 | 38 | /** |
duke@1 | 39 | * A factory that returns a link given the information about it. |
duke@1 | 40 | * |
jjg@1359 | 41 | * <p><b>This is NOT part of any supported API. |
jjg@1359 | 42 | * If you write code that depends on this, you do so at your own risk. |
jjg@1359 | 43 | * This code and its internal interfaces are subject to change or |
jjg@1359 | 44 | * deletion without notice.</b> |
jjg@1359 | 45 | * |
duke@1 | 46 | * @author Jamie Ho |
duke@1 | 47 | * @since 1.5 |
duke@1 | 48 | */ |
duke@1 | 49 | public class LinkFactoryImpl extends LinkFactory { |
duke@1 | 50 | |
duke@1 | 51 | private HtmlDocletWriter m_writer; |
duke@1 | 52 | |
duke@1 | 53 | public LinkFactoryImpl(HtmlDocletWriter writer) { |
duke@1 | 54 | m_writer = writer; |
duke@1 | 55 | } |
duke@1 | 56 | |
duke@1 | 57 | /** |
duke@1 | 58 | * {@inheritDoc} |
duke@1 | 59 | */ |
jjg@1736 | 60 | protected Content newContent() { |
jjg@1736 | 61 | return new ContentBuilder(); |
duke@1 | 62 | } |
duke@1 | 63 | |
duke@1 | 64 | /** |
duke@1 | 65 | * {@inheritDoc} |
duke@1 | 66 | */ |
jjg@1736 | 67 | protected Content getClassLink(LinkInfo linkInfo) { |
duke@1 | 68 | LinkInfoImpl classLinkInfo = (LinkInfoImpl) linkInfo; |
jjg@1737 | 69 | boolean noLabel = linkInfo.label == null || linkInfo.label.isEmpty(); |
duke@1 | 70 | ClassDoc classDoc = classLinkInfo.classDoc; |
duke@1 | 71 | //Create a tool tip if we are linking to a class or interface. Don't |
duke@1 | 72 | //create one if we are linking to a member. |
duke@1 | 73 | String title = |
duke@1 | 74 | (classLinkInfo.where == null || classLinkInfo.where.length() == 0) ? |
duke@1 | 75 | getClassToolTip(classDoc, |
duke@1 | 76 | classLinkInfo.type != null && |
duke@1 | 77 | !classDoc.qualifiedTypeName().equals(classLinkInfo.type.qualifiedTypeName())) : |
duke@1 | 78 | ""; |
jjg@1737 | 79 | Content label = classLinkInfo.getClassLinkLabel(m_writer.configuration); |
jjg@1737 | 80 | classLinkInfo.displayLength += label.charCount(); |
jjg@1410 | 81 | Configuration configuration = m_writer.configuration; |
jjg@1736 | 82 | Content link = new ContentBuilder(); |
duke@1 | 83 | if (classDoc.isIncluded()) { |
duke@1 | 84 | if (configuration.isGeneratedDoc(classDoc)) { |
jjg@1372 | 85 | DocPath filename = getPath(classLinkInfo); |
duke@1 | 86 | if (linkInfo.linkToSelf || |
jjg@1372 | 87 | !(DocPath.forName(classDoc)).equals(m_writer.filename)) { |
jjg@1737 | 88 | link.addContent(m_writer.getHyperLink( |
jjg@1373 | 89 | filename.fragment(classLinkInfo.where), |
jjg@1737 | 90 | label, |
bpatel@182 | 91 | classLinkInfo.isStrong, classLinkInfo.styleName, |
jjg@1737 | 92 | title, classLinkInfo.target)); |
duke@1 | 93 | if (noLabel && !classLinkInfo.excludeTypeParameterLinks) { |
jjg@1736 | 94 | link.addContent(getTypeParameterLinks(linkInfo)); |
duke@1 | 95 | } |
jjg@1736 | 96 | return link; |
duke@1 | 97 | } |
duke@1 | 98 | } |
duke@1 | 99 | } else { |
jjg@1737 | 100 | Content crossLink = m_writer.getCrossClassLink( |
duke@1 | 101 | classDoc.qualifiedName(), classLinkInfo.where, |
jjg@1737 | 102 | label, classLinkInfo.isStrong, classLinkInfo.styleName, |
duke@1 | 103 | true); |
duke@1 | 104 | if (crossLink != null) { |
jjg@1737 | 105 | link.addContent(crossLink); |
duke@1 | 106 | if (noLabel && !classLinkInfo.excludeTypeParameterLinks) { |
jjg@1736 | 107 | link.addContent(getTypeParameterLinks(linkInfo)); |
duke@1 | 108 | } |
jjg@1736 | 109 | return link; |
duke@1 | 110 | } |
duke@1 | 111 | } |
duke@1 | 112 | // Can't link so just write label. |
jjg@1737 | 113 | link.addContent(label.toString()); |
duke@1 | 114 | if (noLabel && !classLinkInfo.excludeTypeParameterLinks) { |
jjg@1736 | 115 | link.addContent(getTypeParameterLinks(linkInfo)); |
duke@1 | 116 | } |
jjg@1736 | 117 | return link; |
duke@1 | 118 | } |
duke@1 | 119 | |
duke@1 | 120 | /** |
duke@1 | 121 | * {@inheritDoc} |
duke@1 | 122 | */ |
jjg@1736 | 123 | protected Content getTypeParameterLink(LinkInfo linkInfo, |
duke@1 | 124 | Type typeParam) { |
jjg@1410 | 125 | LinkInfoImpl typeLinkInfo = new LinkInfoImpl(m_writer.configuration, |
jjg@1735 | 126 | ((LinkInfoImpl) linkInfo).getContext(), typeParam); |
duke@1 | 127 | typeLinkInfo.excludeTypeBounds = linkInfo.excludeTypeBounds; |
duke@1 | 128 | typeLinkInfo.excludeTypeParameterLinks = linkInfo.excludeTypeParameterLinks; |
duke@1 | 129 | typeLinkInfo.linkToSelf = linkInfo.linkToSelf; |
jjg@1521 | 130 | typeLinkInfo.isJava5DeclarationLocation = false; |
jjg@1736 | 131 | Content output = getLink(typeLinkInfo); |
duke@1 | 132 | ((LinkInfoImpl) linkInfo).displayLength += typeLinkInfo.displayLength; |
duke@1 | 133 | return output; |
duke@1 | 134 | } |
duke@1 | 135 | |
jjg@1736 | 136 | protected Content getTypeAnnotationLink(LinkInfo linkInfo, |
jjg@1521 | 137 | AnnotationDesc annotation) { |
jjg@1521 | 138 | throw new RuntimeException("Not implemented yet!"); |
jjg@1521 | 139 | } |
jjg@1521 | 140 | |
jjg@1736 | 141 | public Content getTypeAnnotationLinks(LinkInfo linkInfo) { |
jjg@1736 | 142 | ContentBuilder links = new ContentBuilder(); |
jjg@1521 | 143 | AnnotationDesc[] annotations; |
jjg@1521 | 144 | if (linkInfo.type instanceof AnnotatedType) { |
jjg@1521 | 145 | annotations = linkInfo.type.asAnnotatedType().annotations(); |
jjg@1521 | 146 | } else if (linkInfo.type instanceof TypeVariable) { |
jjg@1521 | 147 | annotations = linkInfo.type.asTypeVariable().annotations(); |
jjg@1521 | 148 | } else { |
jjg@1736 | 149 | return links; |
jjg@1521 | 150 | } |
jjg@1521 | 151 | |
jjg@1521 | 152 | if (annotations.length == 0) |
jjg@1736 | 153 | return links; |
jjg@1521 | 154 | |
jjg@1521 | 155 | List<String> annos = m_writer.getAnnotations(0, annotations, false, linkInfo.isJava5DeclarationLocation); |
jjg@1521 | 156 | |
jjg@1521 | 157 | boolean isFirst = true; |
jjg@1521 | 158 | for (String anno : annos) { |
jjg@1521 | 159 | if (!isFirst) { |
jjg@1521 | 160 | linkInfo.displayLength += 1; |
jjg@1736 | 161 | links.addContent(" "); |
jjg@1521 | 162 | } |
jjg@1736 | 163 | links.addContent(new RawHtml(anno)); |
bpatel@1691 | 164 | isFirst = false; |
jjg@1521 | 165 | } |
jjg@1521 | 166 | if (!annos.isEmpty()) { |
jjg@1521 | 167 | linkInfo.displayLength += 1; |
jjg@1736 | 168 | links.addContent(" "); |
jjg@1521 | 169 | } |
jjg@1521 | 170 | |
jjg@1736 | 171 | return links; |
jjg@1521 | 172 | } |
jjg@1521 | 173 | |
duke@1 | 174 | /** |
duke@1 | 175 | * Given a class, return the appropriate tool tip. |
duke@1 | 176 | * |
duke@1 | 177 | * @param classDoc the class to get the tool tip for. |
duke@1 | 178 | * @return the tool tip for the appropriate class. |
duke@1 | 179 | */ |
duke@1 | 180 | private String getClassToolTip(ClassDoc classDoc, boolean isTypeLink) { |
jjg@1410 | 181 | Configuration configuration = m_writer.configuration; |
duke@1 | 182 | if (isTypeLink) { |
duke@1 | 183 | return configuration.getText("doclet.Href_Type_Param_Title", |
jjg@1410 | 184 | classDoc.name()); |
duke@1 | 185 | } else if (classDoc.isInterface()){ |
duke@1 | 186 | return configuration.getText("doclet.Href_Interface_Title", |
jjg@1736 | 187 | Util.escapeHtmlChars(Util.getPackageName(classDoc.containingPackage()))); |
duke@1 | 188 | } else if (classDoc.isAnnotationType()) { |
duke@1 | 189 | return configuration.getText("doclet.Href_Annotation_Title", |
jjg@1736 | 190 | Util.escapeHtmlChars(Util.getPackageName(classDoc.containingPackage()))); |
duke@1 | 191 | } else if (classDoc.isEnum()) { |
duke@1 | 192 | return configuration.getText("doclet.Href_Enum_Title", |
jjg@1736 | 193 | Util.escapeHtmlChars(Util.getPackageName(classDoc.containingPackage()))); |
duke@1 | 194 | } else { |
duke@1 | 195 | return configuration.getText("doclet.Href_Class_Title", |
jjg@1736 | 196 | Util.escapeHtmlChars(Util.getPackageName(classDoc.containingPackage()))); |
duke@1 | 197 | } |
duke@1 | 198 | } |
duke@1 | 199 | |
duke@1 | 200 | /** |
duke@1 | 201 | * Return path to the given file name in the given package. So if the name |
duke@1 | 202 | * passed is "Object.html" and the name of the package is "java.lang", and |
duke@1 | 203 | * if the relative path is "../.." then returned string will be |
duke@1 | 204 | * "../../java/lang/Object.html" |
duke@1 | 205 | * |
duke@1 | 206 | * @param linkInfo the information about the link. |
duke@1 | 207 | */ |
jjg@1372 | 208 | private DocPath getPath(LinkInfoImpl linkInfo) { |
jjg@1735 | 209 | if (linkInfo.context == LinkInfoImpl.Kind.PACKAGE_FRAME) { |
duke@1 | 210 | //Not really necessary to do this but we want to be consistent |
duke@1 | 211 | //with 1.4.2 output. |
jjg@1372 | 212 | return DocPath.forName(linkInfo.classDoc); |
duke@1 | 213 | } |
jjg@1372 | 214 | return m_writer.pathToRoot.resolve(DocPath.forClass(linkInfo.classDoc)); |
duke@1 | 215 | } |
duke@1 | 216 | } |