src/share/classes/com/sun/tools/doclets/formats/html/TagletWriterImpl.java

Tue, 14 May 2013 10:14:55 -0700

author
jjg
date
Tue, 14 May 2013 10:14:55 -0700
changeset 1745
937aa020c667
parent 1744
76a691e3e961
child 1749
25c89a492f14
permissions
-rw-r--r--

8012177: HTMLDocletWriter methods should generate Content, not Strings
Reviewed-by: darcy

     1 /*
     2  * Copyright (c) 2003, 2013, 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  */
    26 package com.sun.tools.doclets.formats.html;
    28 import com.sun.javadoc.*;
    29 import com.sun.tools.doclets.formats.html.markup.ContentBuilder;
    30 import com.sun.tools.doclets.formats.html.markup.HtmlAttr;
    31 import com.sun.tools.doclets.formats.html.markup.HtmlStyle;
    32 import com.sun.tools.doclets.formats.html.markup.HtmlTag;
    33 import com.sun.tools.doclets.formats.html.markup.HtmlTree;
    34 import com.sun.tools.doclets.formats.html.markup.RawHtml;
    35 import com.sun.tools.doclets.formats.html.markup.StringContent;
    36 import com.sun.tools.doclets.internal.toolkit.*;
    37 import com.sun.tools.doclets.internal.toolkit.builders.SerializedFormBuilder;
    38 import com.sun.tools.doclets.internal.toolkit.taglets.*;
    39 import com.sun.tools.doclets.internal.toolkit.util.*;
    41 /**
    42  * The taglet writer that writes HTML.
    43  *
    44  *  <p><b>This is NOT part of any supported API.
    45  *  If you write code that depends on this, you do so at your own risk.
    46  *  This code and its internal interfaces are subject to change or
    47  *  deletion without notice.</b>
    48  *
    49  * @since 1.5
    50  * @author Jamie Ho
    51  * @author Bhavesh Patel (Modified)
    52  */
    54 public class TagletWriterImpl extends TagletWriter {
    56     private final HtmlDocletWriter htmlWriter;
    57     private final ConfigurationImpl configuration;
    59     public TagletWriterImpl(HtmlDocletWriter htmlWriter, boolean isFirstSentence) {
    60         super(isFirstSentence);
    61         this.htmlWriter = htmlWriter;
    62         configuration = htmlWriter.configuration;
    63     }
    65     /**
    66      * {@inheritDoc}
    67      */
    68     public TagletOutput getOutputInstance() {
    69         return new TagletOutputImpl();
    70     }
    72     /**
    73      * {@inheritDoc}
    74      */
    75     protected TagletOutput codeTagOutput(Tag tag) {
    76         Content result = HtmlTree.CODE(new StringContent(tag.text()));
    77         return new TagletOutputImpl(result);
    78     }
    80     /**
    81      * {@inheritDoc}
    82      */
    83     public TagletOutput getDocRootOutput() {
    84         if (configuration.docrootparent.length() > 0)
    85             return new TagletOutputImpl(configuration.docrootparent);
    86         else if (htmlWriter.pathToRoot.isEmpty())
    87             return new TagletOutputImpl(".");
    88         else
    89             return new TagletOutputImpl(htmlWriter.pathToRoot.getPath());
    90     }
    92     /**
    93      * {@inheritDoc}
    94      */
    95     public TagletOutput deprecatedTagOutput(Doc doc) {
    96         ContentBuilder result = new ContentBuilder();
    97         Tag[] deprs = doc.tags("deprecated");
    98         if (doc instanceof ClassDoc) {
    99             if (Util.isDeprecated((ProgramElementDoc) doc)) {
   100                 result.addContent(HtmlTree.SPAN(HtmlStyle.strong,
   101                         new StringContent(configuration.getText("doclet.Deprecated"))));
   102                 result.addContent(RawHtml.nbsp);
   103                 if (deprs.length > 0) {
   104                     Tag[] commentTags = deprs[0].inlineTags();
   105                     if (commentTags.length > 0) {
   106                         result.addContent(commentTagsToOutput(null, doc,
   107                             deprs[0].inlineTags(), false).getContent()
   108                         );
   109                     }
   110                 }
   111             }
   112         } else {
   113             MemberDoc member = (MemberDoc) doc;
   114             if (Util.isDeprecated((ProgramElementDoc) doc)) {
   115                 result.addContent(HtmlTree.SPAN(HtmlStyle.strong,
   116                         new StringContent(configuration.getText("doclet.Deprecated"))));
   117                 result.addContent(RawHtml.nbsp);
   118                 if (deprs.length > 0) {
   119                     TagletOutputImpl body = commentTagsToOutput(null, doc,
   120                         deprs[0].inlineTags(), false);
   121                     result.addContent(HtmlTree.I(body.getContent()));
   122                 }
   123             } else {
   124                 if (Util.isDeprecated(member.containingClass())) {
   125                     result.addContent(HtmlTree.SPAN(HtmlStyle.strong,
   126                             new StringContent(configuration.getText("doclet.Deprecated"))));
   127                     result.addContent(RawHtml.nbsp);
   128                 }
   129             }
   130         }
   131         return new TagletOutputImpl(result);
   132     }
   134     /**
   135      * {@inheritDoc}
   136      */
   137     protected TagletOutput expertTagOutput(Tag tag) {
   138         HtmlTree result = new HtmlTree(HtmlTag.SUB, new StringContent(tag.text()));
   139         result.addAttr(HtmlAttr.ID, "expert");
   140         return new TagletOutputImpl(result);
   141     }
   143     /**
   144      * {@inheritDoc}
   145      */
   146     protected TagletOutput literalTagOutput(Tag tag) {
   147         Content result = new StringContent(tag.text());
   148         return new TagletOutputImpl(result);
   149     }
   151     /**
   152      * {@inheritDoc}
   153      */
   154     public MessageRetriever getMsgRetriever() {
   155         return configuration.message;
   156     }
   158     /**
   159      * {@inheritDoc}
   160      */
   161     public TagletOutput getParamHeader(String header) {
   162         HtmlTree result = HtmlTree.DT(HtmlTree.SPAN(HtmlStyle.strong,
   163                 new StringContent(header)));
   164         return new TagletOutputImpl(result);
   165     }
   167     /**
   168      * {@inheritDoc}
   169      */
   170     public TagletOutput paramTagOutput(ParamTag paramTag, String paramName) {
   171         ContentBuilder body = new ContentBuilder();
   172         body.addContent(HtmlTree.CODE(new RawHtml(paramName)));
   173         body.addContent(" - ");
   174         body.addContent(htmlWriter.commentTagsToContent(paramTag, null, paramTag.inlineTags(), false));
   175         HtmlTree result = HtmlTree.DD(body);
   176         return new TagletOutputImpl(result);
   177     }
   179     /**
   180      * {@inheritDoc}
   181      */
   182     public TagletOutput returnTagOutput(Tag returnTag) {
   183         ContentBuilder result = new ContentBuilder();
   184         result.addContent(HtmlTree.DT(HtmlTree.SPAN(HtmlStyle.strong,
   185                 new StringContent(configuration.getText("doclet.Returns")))));
   186         result.addContent(HtmlTree.DD(htmlWriter.commentTagsToContent(
   187                 returnTag, null, returnTag.inlineTags(), false)));
   188         return new TagletOutputImpl(result);
   189     }
   191     /**
   192      * {@inheritDoc}
   193      */
   194     public TagletOutput seeTagOutput(Doc holder, SeeTag[] seeTags) {
   195         ContentBuilder body = new ContentBuilder();
   196         if (seeTags.length > 0) {
   197             for (int i = 0; i < seeTags.length; ++i) {
   198                 appendSeparatorIfNotEmpty(body);
   199                 body.addContent(htmlWriter.seeTagToContent(seeTags[i]));
   200             }
   201         }
   202         if (holder.isField() && ((FieldDoc)holder).constantValue() != null &&
   203                 htmlWriter instanceof ClassWriterImpl) {
   204             //Automatically add link to constant values page for constant fields.
   205             appendSeparatorIfNotEmpty(body);
   206             DocPath constantsPath =
   207                     htmlWriter.pathToRoot.resolve(DocPaths.CONSTANT_VALUES);
   208             String whichConstant =
   209                     ((ClassWriterImpl) htmlWriter).getClassDoc().qualifiedName() + "." + ((FieldDoc) holder).name();
   210             DocLink link = constantsPath.fragment(whichConstant);
   211             body.addContent(htmlWriter.getHyperLink(link,
   212                     new StringContent(configuration.getText("doclet.Constants_Summary"))));
   213         }
   214         if (holder.isClass() && ((ClassDoc)holder).isSerializable()) {
   215             //Automatically add link to serialized form page for serializable classes.
   216             if ((SerializedFormBuilder.serialInclude(holder) &&
   217                       SerializedFormBuilder.serialInclude(((ClassDoc)holder).containingPackage()))) {
   218                 appendSeparatorIfNotEmpty(body);
   219                 DocPath serialPath = htmlWriter.pathToRoot.resolve(DocPaths.SERIALIZED_FORM);
   220                 DocLink link = serialPath.fragment(((ClassDoc)holder).qualifiedName());
   221                 body.addContent(htmlWriter.getHyperLink(link,
   222                         new StringContent(configuration.getText("doclet.Serialized_Form"))));
   223             }
   224         }
   225         if (body.isEmpty())
   226             return new TagletOutputImpl(body);
   228         ContentBuilder result = new ContentBuilder();
   229         result.addContent(HtmlTree.DT(HtmlTree.SPAN(HtmlStyle.strong,
   230                 new StringContent(configuration.getText("doclet.See_Also")))));
   231         result.addContent(HtmlTree.DD(body));
   232         return new TagletOutputImpl(result);
   234     }
   236     private void appendSeparatorIfNotEmpty(ContentBuilder body) {
   237         if (!body.isEmpty()) {
   238             body.addContent(", ");
   239             body.addContent(DocletConstants.NL);
   240         }
   241     }
   243     /**
   244      * {@inheritDoc}
   245      */
   246     public TagletOutput simpleTagOutput(Tag[] simpleTags, String header) {
   247         ContentBuilder result = new ContentBuilder();
   248         result.addContent(HtmlTree.DT(HtmlTree.SPAN(HtmlStyle.strong, new RawHtml(header))));
   249         ContentBuilder body = new ContentBuilder();
   250         for (int i = 0; i < simpleTags.length; i++) {
   251             if (i > 0) {
   252                 body.addContent(", ");
   253             }
   254             body.addContent(htmlWriter.commentTagsToContent(
   255                     simpleTags[i], null, simpleTags[i].inlineTags(), false));
   256         }
   257         result.addContent(HtmlTree.DD(body));
   258         return new TagletOutputImpl(result);
   259     }
   261     /**
   262      * {@inheritDoc}
   263      */
   264     public TagletOutput simpleTagOutput(Tag simpleTag, String header) {
   265         ContentBuilder result = new ContentBuilder();
   266         result.addContent(HtmlTree.DT(HtmlTree.SPAN(HtmlStyle.strong, new RawHtml(header))));
   267         Content body = htmlWriter.commentTagsToContent(
   268                 simpleTag, null, simpleTag.inlineTags(), false);
   269         result.addContent(HtmlTree.DD(body));
   270         return new TagletOutputImpl(result);
   271     }
   273     /**
   274      * {@inheritDoc}
   275      */
   276     public TagletOutput getThrowsHeader() {
   277         HtmlTree result = HtmlTree.DT(HtmlTree.SPAN(HtmlStyle.strong,
   278                 new StringContent(configuration.getText("doclet.Throws"))));
   279         return new TagletOutputImpl(result);
   280     }
   282     /**
   283      * {@inheritDoc}
   284      */
   285     public TagletOutput throwsTagOutput(ThrowsTag throwsTag) {
   286         ContentBuilder body = new ContentBuilder();
   287         Content excName = (throwsTag.exceptionType() == null) ?
   288                 new RawHtml(throwsTag.exceptionName()) :
   289                 htmlWriter.getLink(new LinkInfoImpl(configuration, LinkInfoImpl.Kind.MEMBER,
   290                 throwsTag.exceptionType()));
   291         body.addContent(HtmlTree.CODE(excName));
   292         Content desc = htmlWriter.commentTagsToContent(throwsTag, null,
   293             throwsTag.inlineTags(), false);
   294         if (desc != null && !desc.isEmpty()) {
   295             body.addContent(" - ");
   296             body.addContent(desc);
   297         }
   298         HtmlTree res2 = HtmlTree.DD(body);
   299         return new TagletOutputImpl(res2);
   300     }
   302     /**
   303      * {@inheritDoc}
   304      */
   305     public TagletOutput throwsTagOutput(Type throwsType) {
   306         HtmlTree result = HtmlTree.DD(HtmlTree.CODE(htmlWriter.getLink(
   307                 new LinkInfoImpl(configuration, LinkInfoImpl.Kind.MEMBER, throwsType))));
   308         return new TagletOutputImpl(result);
   309     }
   311     /**
   312      * {@inheritDoc}
   313      */
   314     public TagletOutput valueTagOutput(FieldDoc field, String constantVal,
   315             boolean includeLink) {
   316         return new TagletOutputImpl(includeLink ?
   317             htmlWriter.getDocLink(LinkInfoImpl.Kind.VALUE_TAG, field,
   318                 constantVal, false) : new RawHtml(constantVal));
   319     }
   321     /**
   322      * {@inheritDoc}
   323      */
   324     public TagletOutputImpl commentTagsToOutput(Tag holderTag, Tag[] tags) {
   325         return commentTagsToOutput(holderTag, null, tags, false);
   326     }
   328     /**
   329      * {@inheritDoc}
   330      */
   331     public TagletOutputImpl commentTagsToOutput(Doc holderDoc, Tag[] tags) {
   332         return commentTagsToOutput(null, holderDoc, tags, false);
   333     }
   335     /**
   336      * {@inheritDoc}
   337      */
   338     public TagletOutputImpl commentTagsToOutput(Tag holderTag,
   339         Doc holderDoc, Tag[] tags, boolean isFirstSentence) {
   340         return new TagletOutputImpl(htmlWriter.commentTagsToContent(
   341             holderTag, holderDoc, tags, isFirstSentence));
   342     }
   344     /**
   345      * {@inheritDoc}
   346      */
   347     public Configuration configuration() {
   348         return configuration;
   349     }
   350 }

mercurial