8214738: javadoc should honor styles in doc-files

Reviewed-by: jjg
This commit is contained in:
Priya Lakshmi Muthuswamy 2019-01-08 11:16:32 +05:30
parent b1a3c7d538
commit 61c1c09d86
5 changed files with 104 additions and 82 deletions

View File

@ -305,6 +305,8 @@ public enum HtmlTag {
STRONG(BlockType.INLINE, EndKind.REQUIRED,
EnumSet.of(Flag.EXPECT_CONTENT)),
STYLE(BlockType.OTHER, EndKind.REQUIRED),
SUB(BlockType.INLINE, EndKind.REQUIRED,
EnumSet.of(Flag.EXPECT_CONTENT, Flag.NO_NEST)),

View File

@ -31,6 +31,7 @@ import com.sun.source.doctree.DocTree;
import com.sun.source.doctree.EndElementTree;
import com.sun.source.doctree.StartElementTree;
import com.sun.source.doctree.TextTree;
import com.sun.source.util.DocTreeFactory;
import com.sun.source.util.SimpleDocTreeVisitor;
import com.sun.tools.doclint.HtmlTag;
import com.sun.tools.doclint.HtmlTag.Attr;
@ -43,6 +44,7 @@ import jdk.javadoc.internal.doclets.toolkit.util.DocFile;
import jdk.javadoc.internal.doclets.toolkit.util.DocFileIOException;
import jdk.javadoc.internal.doclets.toolkit.util.DocPath;
import jdk.javadoc.internal.doclets.toolkit.util.DocPaths;
import jdk.javadoc.internal.doclets.toolkit.util.DocletConstants;
import jdk.javadoc.internal.doclets.toolkit.util.Utils;
import javax.lang.model.element.Element;
@ -51,6 +53,7 @@ import javax.lang.model.element.PackageElement;
import javax.tools.FileObject;
import javax.tools.JavaFileManager.Location;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -140,13 +143,12 @@ public class DocFilesHandlerImpl implements DocFilesHandler {
srcfile.getPath(), dstdir.getPath());
} else {
if (Utils.toLowerCase(srcfile.getPath()).endsWith(".html")) {
if (handleHtmlFile(srcfile, dstDocPath)) {
continue;
}
handleHtmlFile(srcfile, dstDocPath);
} else {
configuration.messages.notice("doclet.Copying_File_0_To_Dir_1",
srcfile.getPath(), dstdir.getPath());
destfile.copyFile(srcfile);
}
configuration.messages.notice("doclet.Copying_File_0_To_Dir_1",
srcfile.getPath(), dstdir.getPath());
destfile.copyFile(srcfile);
}
} else if (srcfile.isDirectory()) {
if (configuration.copydocfilesubdirs
@ -158,19 +160,18 @@ public class DocFilesHandlerImpl implements DocFilesHandler {
}
}
private boolean handleHtmlFile(DocFile srcfile, DocPath dstPath) throws DocFileIOException {
private void handleHtmlFile(DocFile srcfile, DocPath dstPath) throws DocFileIOException {
Utils utils = configuration.utils;
FileObject fileObject = srcfile.getFileObject();
DocFileElement dfElement = new DocFileElement(element, fileObject);
if (shouldPassThrough(utils.getPreamble(dfElement))) {
return false;
}
DocPath dfilePath = dstPath.resolve(srcfile.getName());
HtmlDocletWriter docletWriter = new DocFileWriter(configuration, dfilePath, element);
configuration.messages.notice("doclet.Generating_0", docletWriter.filename.getPath());
List<? extends DocTree> localTags = getLocalHeaderTags(utils.getPreamble(dfElement));
Content localTagsContent = docletWriter.commentTagsToContent(null, dfElement, localTags, false);
String title = getWindowTitle(docletWriter, dfElement).trim();
HtmlTree htmlContent = docletWriter.getBody(true, title);
docletWriter.addTop(htmlContent);
@ -193,70 +194,61 @@ public class DocFilesHandlerImpl implements DocFilesHandler {
navBar.setUserFooter(docletWriter.getUserHeaderFooter(false));
htmlContent.addContent(navBar.getContent(false));
docletWriter.addBottom(htmlContent);
docletWriter.printHtmlDocument(Collections.emptyList(), false, htmlContent);
return true;
docletWriter.printHtmlDocument(Collections.emptyList(), false, localTagsContent, htmlContent);
}
private boolean shouldPassThrough(List<? extends DocTree> dtrees) {
SimpleDocTreeVisitor<Boolean, Boolean> check = new SimpleDocTreeVisitor<Boolean, Boolean>() {
@Override
public Boolean visitStartElement(StartElementTree node, Boolean p) {
if (Utils.toLowerCase(node.getName().toString()).equals((Attr.STYLE.getText()))) {
return true;
}
if (Utils.toLowerCase(node.getName().toString()).equals(HtmlTag.LINK.getText())) {
for (DocTree dt : node.getAttributes()) {
if (this.visit(dt, true))
return true;
}
}
return false;
}
@Override
public Boolean visitAttribute(AttributeTree node, Boolean p) {
if (p == null || p == false) {
return false;
}
if (Utils.toLowerCase(node.getName().toString()).equals("rel")) {
for (DocTree dt : node.getValue()) {
Boolean found = new SimpleDocTreeVisitor<Boolean, ValueKind>() {
@Override
public Boolean visitText(TextTree node, ValueKind valueKind) {
switch (valueKind) {
case EMPTY:
return false;
default:
return Utils.toLowerCase(node.getBody()).equals("stylesheet");
}
}
@Override
protected Boolean defaultAction(DocTree node, ValueKind valueKind) {
return false;
}
}.visit(dt, node.getValueKind());
if (found)
return true;
}
}
return false;
}
@Override
protected Boolean defaultAction(DocTree node, Boolean p) {
return false;
}
};
private List<? extends DocTree> getLocalHeaderTags(List<? extends DocTree> dtrees) {
List<DocTree> localTags = new ArrayList<>();
DocTreeFactory docTreeFactory = configuration.docEnv.getDocTrees().getDocTreeFactory();
boolean inHead = false;
boolean inTitle = false;
loop:
for (DocTree dt : dtrees) {
if (check.visit(dt, false))
return true;
switch (dt.getKind()) {
case START_ELEMENT:
StartElementTree startElem = (StartElementTree)dt;
switch (HtmlTag.get(startElem.getName())) {
case HEAD:
inHead = true;
break;
case META:
break;
case TITLE:
inTitle = true;
break;
default:
if (inHead) {
localTags.add(startElem);
localTags.add(docTreeFactory.newTextTree(DocletConstants.NL));
}
}
break;
case END_ELEMENT:
EndElementTree endElem = (EndElementTree)dt;
switch (HtmlTag.get(endElem.getName())) {
case HEAD:
inHead = false;
break loop;
case TITLE:
inTitle = false;
break;
default:
if (inHead) {
localTags.add(endElem);
localTags.add(docTreeFactory.newTextTree(DocletConstants.NL));
}
}
break;
case ENTITY:
case TEXT:
if (inHead && !inTitle) {
localTags.add(dt);
}
break;
}
}
return false;
return localTags;
}
private String getWindowTitle(HtmlDocletWriter docletWriter, Element element) {

View File

@ -427,7 +427,24 @@ public class HtmlDocletWriter {
* @throws DocFileIOException if there is a problem writing the file
*/
public void printHtmlDocument(List<String> metakeywords, boolean includeScript,
Content body) throws DocFileIOException {
Content body) throws DocFileIOException {
printHtmlDocument(metakeywords, includeScript, new ContentBuilder(), body);
}
/**
* Generates the HTML document tree and prints it out.
*
* @param metakeywords Array of String keywords for META tag. Each element
* of the array is assigned to a separate META tag.
* Pass in null for no array
* @param includeScript true if printing windowtitle script
* false for files that appear in the left-hand frames
* @param extraContent any additional content to be included in the HEAD element
* @param body the body htmltree to be included in the document
* @throws DocFileIOException if there is a problem writing the file
*/
public void printHtmlDocument(List<String> metakeywords, boolean includeScript, Content extraContent,
Content body) throws DocFileIOException {
DocType htmlDocType = DocType.forVersion(configuration.htmlVersion);
Content htmlComment = contents.newPage;
Head head = new Head(path, configuration.htmlVersion, configuration.docletVersion)
@ -437,7 +454,8 @@ public class HtmlDocletWriter {
.addKeywords(metakeywords)
.setStylesheets(configuration.getMainStylesheet(), configuration.getAdditionalStylesheets())
.setUseModuleDirectories(configuration.useModuleDirectories)
.setIndex(configuration.createindex, mainBodyScript);
.setIndex(configuration.createindex, mainBodyScript)
.addContent(extraContent);
Content htmlTree = HtmlTree.HTML(configuration.getLocale().getLanguage(), head.toContent(), body);
HtmlDocument htmlDocument = new HtmlDocument(htmlDocType, htmlComment, htmlTree);

View File

@ -254,10 +254,6 @@ public class Head {
tree.addContent(HtmlTree.META("keywords", k));
}
for (Content c : extraContent) {
tree.addContent(c);
}
if (canonicalLink != null) {
HtmlTree link = new HtmlTree(HtmlTag.LINK);
link.addAttr(HtmlAttr.REL, "canonical");
@ -267,6 +263,7 @@ public class Head {
addStylesheets(tree);
addScripts(tree);
extraContent.forEach(tree::addContent);
return tree;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,7 +23,7 @@
/*
* @test
* @bug 8157349 8185985 8194953
* @bug 8157349 8185985 8194953 8214738
* @summary test copy of doc-files, and its contents for HTML meta content.
* @library ../../lib
* @modules jdk.javadoc/jdk.javadoc.internal.tool
@ -268,9 +268,22 @@ public class TestCopyFiles extends JavadocTester {
"-sourcepath", testSrc("packages"),
"p2");
checkExit(Exit.OK);
checkOutput("p2/doc-files/case1.html", true, "<!-- Generated by javadoc");
checkOutput("p2/doc-files/case2.html", false, "<!-- Generated by javadoc");
checkOutput("p2/doc-files/case3.html", false, "<!-- Generated by javadoc");
checkOutput("p2/doc-files/case4.html", false, "<!-- Generated by javadoc");
checkOutput("p2/doc-files/case2.html", true,
"<!-- Generated by javadoc",
"<style type=\"text/css\">\n"
+ "body {\n"
+ " font-family: Helvetica, Arial, sans-serif;\n"
+ " font-size: 14px;\n"
+ " }\n"
+ " </style>");
checkOutput("p2/doc-files/case3.html", true,
"<!-- Generated by javadoc",
"<style>\n"
+ "h1 {color:red;}\n"
+ " p {color:blue;}\n"
+ " </style>");
checkOutput("p2/doc-files/case4.html", true,
"<!-- Generated by javadoc",
"<link rel=\"stylesheet\" type=\"text/css\" href=\"theme.css\">");
}
}