1 /*
   2  * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.javadoc.internal.doclets.formats.html;
  27 
  28 import java.util.List;
  29 
  30 import javax.lang.model.element.Element;
  31 import javax.lang.model.element.ModuleElement;
  32 import javax.lang.model.element.PackageElement;
  33 import javax.lang.model.element.TypeElement;
  34 import javax.lang.model.element.VariableElement;
  35 import javax.lang.model.type.TypeMirror;
  36 import javax.lang.model.util.SimpleElementVisitor9;
  37 
  38 import com.sun.source.doctree.DocTree;
  39 import com.sun.source.doctree.IndexTree;
  40 import jdk.javadoc.internal.doclets.formats.html.markup.ContentBuilder;
  41 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle;
  42 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
  43 import jdk.javadoc.internal.doclets.formats.html.markup.RawHtml;
  44 import jdk.javadoc.internal.doclets.formats.html.markup.StringContent;
  45 import jdk.javadoc.internal.doclets.toolkit.BaseConfiguration;
  46 import jdk.javadoc.internal.doclets.toolkit.Content;
  47 import jdk.javadoc.internal.doclets.toolkit.builders.SerializedFormBuilder;
  48 import jdk.javadoc.internal.doclets.toolkit.taglets.TagletWriter;
  49 import jdk.javadoc.internal.doclets.toolkit.util.CommentHelper;
  50 import jdk.javadoc.internal.doclets.toolkit.util.DocLink;
  51 import jdk.javadoc.internal.doclets.toolkit.util.DocPath;
  52 import jdk.javadoc.internal.doclets.toolkit.util.DocPaths;
  53 import jdk.javadoc.internal.doclets.toolkit.util.DocletConstants;
  54 import jdk.javadoc.internal.doclets.toolkit.util.Utils;
  55 
  56 /**
  57  * The taglet writer that writes HTML.
  58  *
  59  *  <p><b>This is NOT part of any supported API.
  60  *  If you write code that depends on this, you do so at your own risk.
  61  *  This code and its internal interfaces are subject to change or
  62  *  deletion without notice.</b>
  63  *
  64  * @author Jamie Ho
  65  * @author Bhavesh Patel (Modified)
  66  */
  67 
  68 public class TagletWriterImpl extends TagletWriter {
  69 
  70     private final HtmlDocletWriter htmlWriter;
  71     private final HtmlConfiguration configuration;
  72     private final Utils utils;
  73 
  74     public TagletWriterImpl(HtmlDocletWriter htmlWriter, boolean isFirstSentence) {
  75         super(isFirstSentence);
  76         this.htmlWriter = htmlWriter;
  77         configuration = htmlWriter.configuration;
  78         this.utils = configuration.utils;
  79     }
  80 
  81     /**
  82      * {@inheritDoc}
  83      */
  84     public Content getOutputInstance() {
  85         return new ContentBuilder();
  86     }
  87 
  88     /**
  89      * {@inheritDoc}
  90      */
  91     protected Content codeTagOutput(Element element, DocTree tag) {
  92         CommentHelper ch = utils.getCommentHelper(element);
  93         StringContent content = new StringContent(utils.normalizeNewlines(ch.getText(tag)));
  94         Content result = HtmlTree.CODE(content);
  95         return result;
  96     }
  97 
  98     protected Content indexTagOutput(Element element, DocTree tag) {
  99         CommentHelper ch = utils.getCommentHelper(element);
 100         IndexTree itt = (IndexTree)tag;
 101 
 102         String tagText =  ch.getText(itt.getSearchTerm());
 103         if (tagText.charAt(0) == '"' && tagText.charAt(tagText.length() - 1) == '"') {
 104             tagText = tagText.substring(1, tagText.length() - 1);
 105         }
 106         String desc = ch.getText(itt.getDescription());
 107 
 108         String anchorName = htmlWriter.getName(tagText);
 109         Content result = HtmlTree.A_ID(HtmlStyle.searchTagResult, anchorName, new StringContent(tagText));
 110         if (configuration.createindex && !tagText.isEmpty()) {
 111             SearchIndexItem si = new SearchIndexItem();
 112             si.setLabel(tagText);
 113             si.setDescription(desc);
 114             new SimpleElementVisitor9<Void, Void>() {
 115                 @Override
 116                 public Void visitModule(ModuleElement e, Void p) {
 117                     si.setUrl(DocPaths.moduleSummary(e).getPath() + "#" + anchorName);
 118                     si.setHolder(utils.getFullyQualifiedName(element));
 119                     return null;
 120                 }
 121 
 122                 @Override
 123                 public Void visitPackage(PackageElement e, Void p) {
 124                     si.setUrl(DocPath.forPackage(e).getPath()
 125                             + "/" + DocPaths.PACKAGE_SUMMARY.getPath() + "#" + anchorName);
 126                     si.setHolder(utils.getSimpleName(element));
 127                     return null;
 128                 }
 129 
 130                 @Override
 131                 public Void visitType(TypeElement e, Void p) {
 132                     si.setUrl(DocPath.forClass(utils, e).getPath() + "#" + anchorName);
 133                     si.setHolder(utils.getFullyQualifiedName(e));
 134                     return null;
 135                 }
 136 
 137                 @Override
 138                 public Void visitVariable(VariableElement e, Void p) {
 139                     TypeElement te = utils.getEnclosingTypeElement(e);
 140                     si.setUrl(DocPath.forClass(utils, te).getPath() + "#" + anchorName);
 141                     si.setHolder(utils.getFullyQualifiedName(e) + "." + utils.getSimpleName(e));
 142                     return null;
 143                 }
 144 
 145                 @Override
 146                 protected Void defaultAction(Element e, Void p) {
 147                     TypeElement te = utils.getEnclosingTypeElement(e);
 148                     si.setUrl(DocPath.forClass(utils, te).getPath() + "#" + anchorName);
 149                     si.setHolder(utils.getFullyQualifiedName(e));
 150                     return null;
 151                 }
 152             }.visit(element);
 153             si.setCategory(configuration.getContent("doclet.SearchTags").toString());
 154             configuration.tagSearchIndex.add(si);
 155         }
 156         return result;
 157     }
 158 
 159     /**
 160      * {@inheritDoc}
 161      */
 162     public Content getDocRootOutput() {
 163         String path;
 164         if (htmlWriter.pathToRoot.isEmpty())
 165             path = ".";
 166         else
 167             path = htmlWriter.pathToRoot.getPath();
 168         return new StringContent(path);
 169     }
 170 
 171     /**
 172      * {@inheritDoc}
 173      */
 174     public Content deprecatedTagOutput(Element element) {
 175         ContentBuilder result = new ContentBuilder();
 176         CommentHelper ch = utils.getCommentHelper(element);
 177         List<? extends DocTree> deprs = utils.getBlockTags(element, DocTree.Kind.DEPRECATED);
 178         if (utils.isTypeElement(element)) {
 179             if (utils.isDeprecated(element)) {
 180                 result.addContent(HtmlTree.SPAN(HtmlStyle.deprecatedLabel,
 181                         htmlWriter.getDeprecatedPhrase(element)));
 182                 if (!deprs.isEmpty()) {
 183                     List<? extends DocTree> commentTags = ch.getDescription(configuration, deprs.get(0));
 184                     if (!commentTags.isEmpty()) {
 185                         result.addContent(commentTagsToOutput(null, element, commentTags, false));
 186                     }
 187                 }
 188             }
 189         } else {
 190             if (utils.isDeprecated(element)) {
 191                 result.addContent(HtmlTree.SPAN(HtmlStyle.deprecatedLabel,
 192                         htmlWriter.getDeprecatedPhrase(element)));
 193                 if (!deprs.isEmpty()) {
 194                     List<? extends DocTree> bodyTags = ch.getBody(configuration, deprs.get(0));
 195                     Content body = commentTagsToOutput(null, element, bodyTags, false);
 196                     if (!body.isEmpty())
 197                         result.addContent(HtmlTree.DIV(HtmlStyle.deprecationComment, body));
 198                 }
 199             } else {
 200                 Element ee = utils.getEnclosingTypeElement(element);
 201                 if (utils.isDeprecated(ee)) {
 202                     result.addContent(HtmlTree.SPAN(HtmlStyle.deprecatedLabel,
 203                         htmlWriter.getDeprecatedPhrase(ee)));
 204                 }
 205             }
 206         }
 207         return result;
 208     }
 209 
 210     /**
 211      * {@inheritDoc}
 212      */
 213     protected Content literalTagOutput(Element element, DocTree tag) {
 214         CommentHelper ch = utils.getCommentHelper(element);
 215         Content result = new StringContent(utils.normalizeNewlines(ch.getText(tag)));
 216         return result;
 217     }
 218 
 219     /**
 220      * {@inheritDoc}
 221      */
 222     public Content getParamHeader(String header) {
 223         HtmlTree result = HtmlTree.DT(HtmlTree.SPAN(HtmlStyle.paramLabel,
 224                 new StringContent(header)));
 225         return result;
 226     }
 227 
 228     /**
 229      * {@inheritDoc}
 230      */
 231     public Content paramTagOutput(Element element, DocTree paramTag, String paramName) {
 232         ContentBuilder body = new ContentBuilder();
 233         CommentHelper ch = utils.getCommentHelper(element);
 234         body.addContent(HtmlTree.CODE(new RawHtml(paramName)));
 235         body.addContent(" - ");
 236         List<? extends DocTree> description = ch.getDescription(configuration, paramTag);
 237         body.addContent(htmlWriter.commentTagsToContent(paramTag, element, description, false));
 238         HtmlTree result = HtmlTree.DD(body);
 239         return result;
 240     }
 241 
 242     /**
 243      * {@inheritDoc}
 244      */
 245     public Content propertyTagOutput(Element element, DocTree tag, String prefix) {
 246         Content body = new ContentBuilder();
 247         CommentHelper ch = utils.getCommentHelper(element);
 248         body.addContent(new RawHtml(prefix));
 249         body.addContent(" ");
 250         body.addContent(HtmlTree.CODE(new RawHtml(ch.getText(tag))));
 251         body.addContent(".");
 252         Content result = HtmlTree.P(body);
 253         return result;
 254     }
 255 
 256     /**
 257      * {@inheritDoc}
 258      */
 259     public Content returnTagOutput(Element element, DocTree returnTag) {
 260         ContentBuilder result = new ContentBuilder();
 261         CommentHelper ch = utils.getCommentHelper(element);
 262         result.addContent(HtmlTree.DT(HtmlTree.SPAN(HtmlStyle.returnLabel,
 263                 new StringContent(configuration.getText("doclet.Returns")))));
 264         result.addContent(HtmlTree.DD(htmlWriter.commentTagsToContent(
 265                 returnTag, element, ch.getDescription(configuration, returnTag), false)));
 266         return result;
 267     }
 268 
 269     /**
 270      * {@inheritDoc}
 271      */
 272     public Content seeTagOutput(Element holder, List<? extends DocTree> seeTags) {
 273         ContentBuilder body = new ContentBuilder();
 274         if (!seeTags.isEmpty()) {
 275             for (DocTree dt : seeTags) {
 276                 appendSeparatorIfNotEmpty(body);
 277                 body.addContent(htmlWriter.seeTagToContent(holder, dt));
 278             }
 279         }
 280         if (utils.isVariableElement(holder) && ((VariableElement)holder).getConstantValue() != null &&
 281                 htmlWriter instanceof ClassWriterImpl) {
 282             //Automatically add link to constant values page for constant fields.
 283             appendSeparatorIfNotEmpty(body);
 284             DocPath constantsPath =
 285                     htmlWriter.pathToRoot.resolve(DocPaths.CONSTANT_VALUES);
 286             String whichConstant =
 287                     ((ClassWriterImpl) htmlWriter).getTypeElement().getQualifiedName() + "." +
 288                     utils.getSimpleName(holder);
 289             DocLink link = constantsPath.fragment(whichConstant);
 290             body.addContent(htmlWriter.getHyperLink(link,
 291                     new StringContent(configuration.getText("doclet.Constants_Summary"))));
 292         }
 293         if (utils.isClass(holder) && utils.isSerializable((TypeElement)holder)) {
 294             //Automatically add link to serialized form page for serializable classes.
 295             if (SerializedFormBuilder.serialInclude(utils, holder) &&
 296                       SerializedFormBuilder.serialInclude(utils, utils.containingPackage(holder))) {
 297                 appendSeparatorIfNotEmpty(body);
 298                 DocPath serialPath = htmlWriter.pathToRoot.resolve(DocPaths.SERIALIZED_FORM);
 299                 DocLink link = serialPath.fragment(utils.getFullyQualifiedName(holder));
 300                 body.addContent(htmlWriter.getHyperLink(link,
 301                         new StringContent(configuration.getText("doclet.Serialized_Form"))));
 302             }
 303         }
 304         if (body.isEmpty())
 305             return body;
 306 
 307         ContentBuilder result = new ContentBuilder();
 308         result.addContent(HtmlTree.DT(HtmlTree.SPAN(HtmlStyle.seeLabel,
 309                 new StringContent(configuration.getText("doclet.See_Also")))));
 310         result.addContent(HtmlTree.DD(body));
 311         return result;
 312 
 313     }
 314 
 315     private void appendSeparatorIfNotEmpty(ContentBuilder body) {
 316         if (!body.isEmpty()) {
 317             body.addContent(", ");
 318             body.addContent(DocletConstants.NL);
 319         }
 320     }
 321 
 322     /**
 323      * {@inheritDoc}
 324      */
 325     public Content simpleTagOutput(Element element, List<? extends DocTree> simpleTags, String header) {
 326         CommentHelper ch = utils.getCommentHelper(element);
 327         ContentBuilder result = new ContentBuilder();
 328         result.addContent(HtmlTree.DT(HtmlTree.SPAN(HtmlStyle.simpleTagLabel, new RawHtml(header))));
 329         ContentBuilder body = new ContentBuilder();
 330         boolean many = false;
 331         for (DocTree simpleTag : simpleTags) {
 332             if (many) {
 333                 body.addContent(", ");
 334             }
 335             List<? extends DocTree> bodyTags = ch.getBody(configuration, simpleTag);
 336             body.addContent(htmlWriter.commentTagsToContent(simpleTag, element, bodyTags, false));
 337             many = true;
 338         }
 339         result.addContent(HtmlTree.DD(body));
 340         return result;
 341     }
 342 
 343     /**
 344      * {@inheritDoc}
 345      */
 346     public Content simpleTagOutput(Element element, DocTree simpleTag, String header) {
 347         ContentBuilder result = new ContentBuilder();
 348         result.addContent(HtmlTree.DT(HtmlTree.SPAN(HtmlStyle.simpleTagLabel, new RawHtml(header))));
 349         CommentHelper ch = utils.getCommentHelper(element);
 350         List<? extends DocTree> description = ch.getDescription(configuration, simpleTag);
 351         Content body = htmlWriter.commentTagsToContent(simpleTag, element, description, false);
 352         result.addContent(HtmlTree.DD(body));
 353         return result;
 354     }
 355 
 356     /**
 357      * {@inheritDoc}
 358      */
 359     public Content getThrowsHeader() {
 360         HtmlTree result = HtmlTree.DT(HtmlTree.SPAN(HtmlStyle.throwsLabel,
 361                 new StringContent(configuration.getText("doclet.Throws"))));
 362         return result;
 363     }
 364 
 365     /**
 366      * {@inheritDoc}
 367      */
 368     public Content throwsTagOutput(Element element, DocTree throwsTag) {
 369         ContentBuilder body = new ContentBuilder();
 370         CommentHelper ch = utils.getCommentHelper(element);
 371         Element exception = ch.getException(configuration, throwsTag);
 372         Content excName;
 373         if (exception == null) {
 374             excName = new RawHtml(ch.getExceptionName(throwsTag).toString());
 375         } else if (exception.asType() == null) {
 376             excName = new RawHtml(utils.getFullyQualifiedName(exception));
 377         } else {
 378             LinkInfoImpl link = new LinkInfoImpl(configuration, LinkInfoImpl.Kind.MEMBER,
 379                                                  exception.asType());
 380             link.excludeTypeBounds = true;
 381             excName = htmlWriter.getLink(link);
 382         }
 383         body.addContent(HtmlTree.CODE(excName));
 384         List<? extends DocTree> description = ch.getDescription(configuration, throwsTag);
 385         Content desc = htmlWriter.commentTagsToContent(throwsTag, element, description, false);
 386         if (desc != null && !desc.isEmpty()) {
 387             body.addContent(" - ");
 388             body.addContent(desc);
 389         }
 390         HtmlTree result = HtmlTree.DD(body);
 391         return result;
 392     }
 393 
 394     /**
 395      * {@inheritDoc}
 396      */
 397     public Content throwsTagOutput(TypeMirror throwsType) {
 398         HtmlTree result = HtmlTree.DD(HtmlTree.CODE(htmlWriter.getLink(
 399                 new LinkInfoImpl(configuration, LinkInfoImpl.Kind.MEMBER, throwsType))));
 400         return result;
 401     }
 402 
 403     /**
 404      * {@inheritDoc}
 405      */
 406     public Content valueTagOutput(VariableElement field, String constantVal, boolean includeLink) {
 407         return includeLink ?
 408             htmlWriter.getDocLink(LinkInfoImpl.Kind.VALUE_TAG, field,
 409                 constantVal, false) : new RawHtml(constantVal);
 410     }
 411 
 412     /**
 413      * {@inheritDoc}
 414      */
 415     public Content commentTagsToOutput(DocTree holderTag, List<? extends DocTree> tags) {
 416         return commentTagsToOutput(holderTag, null, tags, false);
 417     }
 418 
 419     /**
 420      * {@inheritDoc}
 421      */
 422     public Content commentTagsToOutput(Element holder, List<? extends DocTree> tags) {
 423         return commentTagsToOutput(null, holder, tags, false);
 424     }
 425 
 426     /**
 427      * {@inheritDoc}
 428      */
 429     public Content commentTagsToOutput(DocTree holderTag,
 430         Element holder, List<? extends DocTree> tags, boolean isFirstSentence) {
 431         return htmlWriter.commentTagsToContent(holderTag, holder,
 432                 tags, isFirstSentence);
 433     }
 434 
 435     /**
 436      * {@inheritDoc}
 437      */
 438     public BaseConfiguration configuration() {
 439         return configuration;
 440     }
 441 }