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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.doclets.formats.html.markup;
aoqi@0 27
aoqi@0 28 import java.io.*;
aoqi@0 29 import java.util.*;
aoqi@0 30
aoqi@0 31 import com.sun.javadoc.*;
aoqi@0 32 import com.sun.tools.doclets.formats.html.ConfigurationImpl;
aoqi@0 33 import com.sun.tools.doclets.formats.html.SectionName;
aoqi@0 34 import com.sun.tools.doclets.internal.toolkit.*;
aoqi@0 35 import com.sun.tools.doclets.internal.toolkit.util.DocFile;
aoqi@0 36 import com.sun.tools.doclets.internal.toolkit.util.DocLink;
aoqi@0 37 import com.sun.tools.doclets.internal.toolkit.util.DocPath;
aoqi@0 38
aoqi@0 39
aoqi@0 40 /**
aoqi@0 41 * Class for the Html Format Code Generation specific to JavaDoc.
aoqi@0 42 * This Class contains methods related to the Html Code Generation which
aoqi@0 43 * are used by the Sub-Classes in the package com.sun.tools.doclets.standard
aoqi@0 44 * and com.sun.tools.doclets.oneone.
aoqi@0 45 *
aoqi@0 46 * <p><b>This is NOT part of any supported API.
aoqi@0 47 * If you write code that depends on this, you do so at your own risk.
aoqi@0 48 * This code and its internal interfaces are subject to change or
aoqi@0 49 * deletion without notice.</b>
aoqi@0 50 *
aoqi@0 51 * @since 1.2
aoqi@0 52 * @author Atul M Dambalkar
aoqi@0 53 * @author Robert Field
aoqi@0 54 */
aoqi@0 55 public abstract class HtmlDocWriter extends HtmlWriter {
aoqi@0 56
aoqi@0 57 public static final String CONTENT_TYPE = "text/html";
aoqi@0 58
aoqi@0 59 /**
aoqi@0 60 * Constructor. Initializes the destination file name through the super
aoqi@0 61 * class HtmlWriter.
aoqi@0 62 *
aoqi@0 63 * @param filename String file name.
aoqi@0 64 */
aoqi@0 65 public HtmlDocWriter(Configuration configuration, DocPath filename)
aoqi@0 66 throws IOException {
aoqi@0 67 super(configuration, filename);
aoqi@0 68 configuration.message.notice("doclet.Generating_0",
aoqi@0 69 DocFile.createFileForOutput(configuration, filename).getPath());
aoqi@0 70 }
aoqi@0 71
aoqi@0 72 /**
aoqi@0 73 * Accessor for configuration.
aoqi@0 74 */
aoqi@0 75 public abstract Configuration configuration();
aoqi@0 76
aoqi@0 77 public Content getHyperLink(DocPath link, String label) {
aoqi@0 78 return getHyperLink(link, new StringContent(label), false, "", "", "");
aoqi@0 79 }
aoqi@0 80
aoqi@0 81 /**
aoqi@0 82 * Get Html Hyper Link Content.
aoqi@0 83 *
aoqi@0 84 * @param where Position of the link in the file. Character '#' is not
aoqi@0 85 * needed.
aoqi@0 86 * @param label Tag for the link.
aoqi@0 87 * @return a content tree for the hyper link
aoqi@0 88 */
aoqi@0 89 public Content getHyperLink(String where,
aoqi@0 90 Content label) {
aoqi@0 91 return getHyperLink(getDocLink(where), label, "", "");
aoqi@0 92 }
aoqi@0 93
aoqi@0 94 /**
aoqi@0 95 * Get Html Hyper Link Content.
aoqi@0 96 *
aoqi@0 97 * @param sectionName The section name to which the link will be created.
aoqi@0 98 * @param label Tag for the link.
aoqi@0 99 * @return a content tree for the hyper link
aoqi@0 100 */
aoqi@0 101 public Content getHyperLink(SectionName sectionName,
aoqi@0 102 Content label) {
aoqi@0 103 return getHyperLink(getDocLink(sectionName), label, "", "");
aoqi@0 104 }
aoqi@0 105
aoqi@0 106 /**
aoqi@0 107 * Get Html Hyper Link Content.
aoqi@0 108 *
aoqi@0 109 * @param sectionName The section name combined with where to which the link
aoqi@0 110 * will be created.
aoqi@0 111 * @param where The fragment combined with sectionName to which the link
aoqi@0 112 * will be created.
aoqi@0 113 * @param label Tag for the link.
aoqi@0 114 * @return a content tree for the hyper link
aoqi@0 115 */
aoqi@0 116 public Content getHyperLink(SectionName sectionName, String where,
aoqi@0 117 Content label) {
aoqi@0 118 return getHyperLink(getDocLink(sectionName, where), label, "", "");
aoqi@0 119 }
aoqi@0 120
aoqi@0 121 /**
aoqi@0 122 * Get the link.
aoqi@0 123 *
aoqi@0 124 * @param where Position of the link in the file.
aoqi@0 125 * @return a DocLink object for the hyper link
aoqi@0 126 */
aoqi@0 127 public DocLink getDocLink(String where) {
aoqi@0 128 return DocLink.fragment(getName(where));
aoqi@0 129 }
aoqi@0 130
aoqi@0 131 /**
aoqi@0 132 * Get the link.
aoqi@0 133 *
aoqi@0 134 * @param sectionName The section name to which the link will be created.
aoqi@0 135 * @return a DocLink object for the hyper link
aoqi@0 136 */
aoqi@0 137 public DocLink getDocLink(SectionName sectionName) {
aoqi@0 138 return DocLink.fragment(sectionName.getName());
aoqi@0 139 }
aoqi@0 140
aoqi@0 141 /**
aoqi@0 142 * Get the link.
aoqi@0 143 *
aoqi@0 144 * @param sectionName The section name combined with where to which the link
aoqi@0 145 * will be created.
aoqi@0 146 * @param where The fragment combined with sectionName to which the link
aoqi@0 147 * will be created.
aoqi@0 148 * @return a DocLink object for the hyper link
aoqi@0 149 */
aoqi@0 150 public DocLink getDocLink(SectionName sectionName, String where) {
aoqi@0 151 return DocLink.fragment(sectionName.getName() + getName(where));
aoqi@0 152 }
aoqi@0 153
aoqi@0 154 /**
aoqi@0 155 * Convert the name to a valid HTML name.
aoqi@0 156 *
aoqi@0 157 * @param name the name that needs to be converted to valid HTML name.
aoqi@0 158 * @return a valid HTML name string.
aoqi@0 159 */
aoqi@0 160 public String getName(String name) {
aoqi@0 161 StringBuilder sb = new StringBuilder();
aoqi@0 162 char ch;
aoqi@0 163 /* The HTML 4 spec at http://www.w3.org/TR/html4/types.html#h-6.2 mentions
aoqi@0 164 * that the name/id should begin with a letter followed by other valid characters.
aoqi@0 165 * The HTML 5 spec (draft) is more permissive on names/ids where the only restriction
aoqi@0 166 * is that it should be at least one character long and should not contain spaces.
aoqi@0 167 * The spec draft is @ http://www.w3.org/html/wg/drafts/html/master/dom.html#the-id-attribute.
aoqi@0 168 *
aoqi@0 169 * For HTML 4, we need to check for non-characters at the beginning of the name and
aoqi@0 170 * substitute it accordingly, "_" and "$" can appear at the beginning of a member name.
aoqi@0 171 * The method substitutes "$" with "Z:Z:D" and will prefix "_" with "Z:Z".
aoqi@0 172 */
aoqi@0 173 for (int i = 0; i < name.length(); i++) {
aoqi@0 174 ch = name.charAt(i);
aoqi@0 175 switch (ch) {
aoqi@0 176 case '(':
aoqi@0 177 case ')':
aoqi@0 178 case '<':
aoqi@0 179 case '>':
aoqi@0 180 case ',':
aoqi@0 181 sb.append('-');
aoqi@0 182 break;
aoqi@0 183 case ' ':
aoqi@0 184 case '[':
aoqi@0 185 break;
aoqi@0 186 case ']':
aoqi@0 187 sb.append(":A");
aoqi@0 188 break;
aoqi@0 189 // Any appearance of $ needs to be substituted with ":D" and not with hyphen
aoqi@0 190 // since a field name "P$$ and a method P(), both valid member names, can end
aoqi@0 191 // up as "P--". A member name beginning with $ needs to be substituted with
aoqi@0 192 // "Z:Z:D".
aoqi@0 193 case '$':
aoqi@0 194 if (i == 0)
aoqi@0 195 sb.append("Z:Z");
aoqi@0 196 sb.append(":D");
aoqi@0 197 break;
aoqi@0 198 // A member name beginning with _ needs to be prefixed with "Z:Z" since valid anchor
aoqi@0 199 // names can only begin with a letter.
aoqi@0 200 case '_':
aoqi@0 201 if (i == 0)
aoqi@0 202 sb.append("Z:Z");
aoqi@0 203 sb.append(ch);
aoqi@0 204 break;
aoqi@0 205 default:
aoqi@0 206 sb.append(ch);
aoqi@0 207 }
aoqi@0 208 }
aoqi@0 209 return sb.toString();
aoqi@0 210 }
aoqi@0 211
aoqi@0 212 /**
aoqi@0 213 * Get Html hyperlink.
aoqi@0 214 *
aoqi@0 215 * @param link path of the file.
aoqi@0 216 * @param label Tag for the link.
aoqi@0 217 * @return a content tree for the hyper link
aoqi@0 218 */
aoqi@0 219 public Content getHyperLink(DocPath link, Content label) {
aoqi@0 220 return getHyperLink(link, label, "", "");
aoqi@0 221 }
aoqi@0 222
aoqi@0 223 public Content getHyperLink(DocLink link, Content label) {
aoqi@0 224 return getHyperLink(link, label, "", "");
aoqi@0 225 }
aoqi@0 226
aoqi@0 227 public Content getHyperLink(DocPath link,
aoqi@0 228 Content label, boolean strong,
aoqi@0 229 String stylename, String title, String target) {
aoqi@0 230 return getHyperLink(new DocLink(link), label, strong,
aoqi@0 231 stylename, title, target);
aoqi@0 232 }
aoqi@0 233
aoqi@0 234 public Content getHyperLink(DocLink link,
aoqi@0 235 Content label, boolean strong,
aoqi@0 236 String stylename, String title, String target) {
aoqi@0 237 Content body = label;
aoqi@0 238 if (strong) {
aoqi@0 239 body = HtmlTree.SPAN(HtmlStyle.typeNameLink, body);
aoqi@0 240 }
aoqi@0 241 if (stylename != null && stylename.length() != 0) {
aoqi@0 242 HtmlTree t = new HtmlTree(HtmlTag.FONT, body);
aoqi@0 243 t.addAttr(HtmlAttr.CLASS, stylename);
aoqi@0 244 body = t;
aoqi@0 245 }
aoqi@0 246 HtmlTree l = HtmlTree.A(link.toString(), body);
aoqi@0 247 if (title != null && title.length() != 0) {
aoqi@0 248 l.addAttr(HtmlAttr.TITLE, title);
aoqi@0 249 }
aoqi@0 250 if (target != null && target.length() != 0) {
aoqi@0 251 l.addAttr(HtmlAttr.TARGET, target);
aoqi@0 252 }
aoqi@0 253 return l;
aoqi@0 254 }
aoqi@0 255
aoqi@0 256 /**
aoqi@0 257 * Get Html Hyper Link.
aoqi@0 258 *
aoqi@0 259 * @param link String name of the file.
aoqi@0 260 * @param label Tag for the link.
aoqi@0 261 * @param title String that describes the link's content for accessibility.
aoqi@0 262 * @param target Target frame.
aoqi@0 263 * @return a content tree for the hyper link.
aoqi@0 264 */
aoqi@0 265 public Content getHyperLink(DocPath link,
aoqi@0 266 Content label, String title, String target) {
aoqi@0 267 return getHyperLink(new DocLink(link), label, title, target);
aoqi@0 268 }
aoqi@0 269
aoqi@0 270 public Content getHyperLink(DocLink link,
aoqi@0 271 Content label, String title, String target) {
aoqi@0 272 HtmlTree anchor = HtmlTree.A(link.toString(), label);
aoqi@0 273 if (title != null && title.length() != 0) {
aoqi@0 274 anchor.addAttr(HtmlAttr.TITLE, title);
aoqi@0 275 }
aoqi@0 276 if (target != null && target.length() != 0) {
aoqi@0 277 anchor.addAttr(HtmlAttr.TARGET, target);
aoqi@0 278 }
aoqi@0 279 return anchor;
aoqi@0 280 }
aoqi@0 281
aoqi@0 282 /**
aoqi@0 283 * Get the name of the package, this class is in.
aoqi@0 284 *
aoqi@0 285 * @param cd ClassDoc.
aoqi@0 286 */
aoqi@0 287 public String getPkgName(ClassDoc cd) {
aoqi@0 288 String pkgName = cd.containingPackage().name();
aoqi@0 289 if (pkgName.length() > 0) {
aoqi@0 290 pkgName += ".";
aoqi@0 291 return pkgName;
aoqi@0 292 }
aoqi@0 293 return "";
aoqi@0 294 }
aoqi@0 295
aoqi@0 296 public boolean getMemberDetailsListPrinted() {
aoqi@0 297 return memberDetailsListPrinted;
aoqi@0 298 }
aoqi@0 299
aoqi@0 300 /**
aoqi@0 301 * Print the frameset version of the Html file header.
aoqi@0 302 * Called only when generating an HTML frameset file.
aoqi@0 303 *
aoqi@0 304 * @param title Title of this HTML document
aoqi@0 305 * @param noTimeStamp If true, don't print time stamp in header
aoqi@0 306 * @param frameset the frameset to be added to the HTML document
aoqi@0 307 */
aoqi@0 308 public void printFramesetDocument(String title, boolean noTimeStamp,
aoqi@0 309 Content frameset) throws IOException {
aoqi@0 310 Content htmlDocType = DocType.FRAMESET;
aoqi@0 311 Content htmlComment = new Comment(configuration.getText("doclet.New_Page"));
aoqi@0 312 Content head = new HtmlTree(HtmlTag.HEAD);
aoqi@0 313 head.addContent(getGeneratedBy(!noTimeStamp));
aoqi@0 314 if (configuration.charset.length() > 0) {
aoqi@0 315 Content meta = HtmlTree.META("Content-Type", CONTENT_TYPE,
aoqi@0 316 configuration.charset);
aoqi@0 317 head.addContent(meta);
aoqi@0 318 }
aoqi@0 319 Content windowTitle = HtmlTree.TITLE(new StringContent(title));
aoqi@0 320 head.addContent(windowTitle);
aoqi@0 321 head.addContent(getFramesetJavaScript());
aoqi@0 322 Content htmlTree = HtmlTree.HTML(configuration.getLocale().getLanguage(),
aoqi@0 323 head, frameset);
aoqi@0 324 Content htmlDocument = new HtmlDocument(htmlDocType,
aoqi@0 325 htmlComment, htmlTree);
aoqi@0 326 write(htmlDocument);
aoqi@0 327 }
aoqi@0 328
aoqi@0 329 protected Comment getGeneratedBy(boolean timestamp) {
aoqi@0 330 String text = "Generated by javadoc"; // marker string, deliberately not localized
aoqi@0 331 if (timestamp) {
aoqi@0 332 Calendar calendar = new GregorianCalendar(TimeZone.getDefault());
aoqi@0 333 Date today = calendar.getTime();
aoqi@0 334 text += " ("+ configuration.getDocletSpecificBuildDate() + ") on " + today;
aoqi@0 335 }
aoqi@0 336 return new Comment(text);
aoqi@0 337 }
aoqi@0 338 }

mercurial