8185371: Support for multiple stylesheets in javadoc

Reviewed-by: jjg, ksrini
This commit is contained in:
Bhavesh Patel 2017-11-14 13:44:07 -08:00
parent 304348561f
commit 616491477f
13 changed files with 156 additions and 16 deletions

View File

@ -139,7 +139,7 @@ public class FrameOutputWriter extends HtmlDocletWriter {
head.addContent(windowTitle);
Content meta = HtmlTree.META("Content-Type", CONTENT_TYPE, configuration.charset);
head.addContent(meta);
head.addContent(getStyleSheetProperties(configuration));
addStyleSheetProperties(configuration, head);
head.addContent(getFramesJavaScript());
Content htmlTree = HtmlTree.HTML(configuration.getLocale().getLanguage(),
head, body);

View File

@ -132,6 +132,11 @@ public class HtmlConfiguration extends BaseConfiguration {
*/
public String stylesheetfile = "";
/**
* Argument for command line option "--add-stylesheet".
*/
public List<String> additionalStylesheets = new ArrayList<>();
/**
* Argument for command line option "-Xdocrootparent".
*/
@ -304,6 +309,22 @@ public class HtmlConfiguration extends BaseConfiguration {
return false;
}
}
// check if stylesheetfile exists
if (!stylesheetfile.isEmpty()) {
DocFile stylesheet = DocFile.createFileForInput(this, stylesheetfile);
if (!stylesheet.exists()) {
reporter.print(ERROR, getText("doclet.File_not_found", stylesheetfile));
return false;
}
}
// check if additional stylesheets exists
for (String ssheet : additionalStylesheets) {
DocFile ssfile = DocFile.createFileForInput(this, ssheet);
if (!ssfile.exists()) {
reporter.print(ERROR, getText("doclet.File_not_found", ssheet));
return false;
}
}
// In a more object-oriented world, this would be done by methods on the Option objects.
// Note that -windowtitle silently removes any and all HTML elements, and so does not need
@ -554,6 +575,13 @@ public class HtmlConfiguration extends BaseConfiguration {
public Set<Doclet.Option> getSupportedOptions() {
Resources resources = getResources();
Doclet.Option[] options = {
new Option(resources, "--add-stylesheet", 1) {
@Override
public boolean process(String opt, List<String> args) {
additionalStylesheets.add(args.get(0));
return true;
}
},
new Option(resources, "-bottom", 1) {
@Override
public boolean process(String opt, List<String> args) {
@ -722,7 +750,7 @@ public class HtmlConfiguration extends BaseConfiguration {
return true;
}
},
new Option(resources, "-stylesheetfile", 1) {
new Option(resources, "--main-stylesheet -stylesheetfile", 1) {
@Override
public boolean process(String opt, List<String> args) {
stylesheetfile = args.get(0);

View File

@ -125,6 +125,9 @@ public class HtmlDoclet extends AbstractDoclet {
boolean nodeprecated = configuration.nodeprecated;
performCopy(configuration.helpfile);
performCopy(configuration.stylesheetfile);
for (String stylesheet : configuration.additionalStylesheets) {
performCopy(stylesheet);
}
// do early to reduce memory footprint
if (configuration.classuse) {
ClassUseWriter.generate(configuration, classtree);

View File

@ -2162,6 +2162,7 @@ public class HtmlDocletWriter extends HtmlDocWriter {
pathToRoot.resolve(stylesheet).getPath(),
"Style");
head.addContent(link);
addStylesheets(configuration, head);
if (configuration.createindex) {
HtmlTree jq_link = HtmlTree.LINK("stylesheet", "text/css",
pathToRoot.resolve(DocPaths.JQUERY_FILES.resolve(DocPaths.JQUERY_STYLESHEET_FILE)).getPath(),

View File

@ -94,7 +94,7 @@ public class IndexRedirectWriter extends HtmlDocletWriter {
head.addContent(metaRefresh);
}
head.addContent(getStyleSheetProperties(configuration));
addStyleSheetProperties(configuration, head);
ContentBuilder bodyContent = new ContentBuilder();
bodyContent.addContent(HtmlTree.NOSCRIPT(

View File

@ -26,6 +26,7 @@
package jdk.javadoc.internal.doclets.formats.html;
import java.io.*;
import java.util.List;
import javax.lang.model.element.Element;
import javax.lang.model.element.PackageElement;
@ -210,7 +211,7 @@ public class SourceToHTMLConverter {
Content head = new HtmlTree(HtmlTag.HEAD);
head.addContent(HtmlTree.TITLE(new StringContent(
configuration.getText("doclet.Window_Source_title"))));
head.addContent(getStyleSheetProperties());
addStyleSheetProperties(head);
Content htmlTree = HtmlTree.HTML(configuration.getLocale().getLanguage(),
head, body);
Content htmlDocument = new HtmlDocument(htmlDocType, htmlTree);
@ -227,9 +228,9 @@ public class SourceToHTMLConverter {
/**
* Returns a link to the stylesheet file.
*
* @return an HtmlTree for the lINK tag which provides the stylesheet location
* @param head an HtmlTree to which the stylesheet links will be added
*/
public HtmlTree getStyleSheetProperties() {
public void addStyleSheetProperties(Content head) {
String filename = configuration.stylesheetfile;
DocPath stylesheet;
if (filename.length() > 0) {
@ -240,7 +241,21 @@ public class SourceToHTMLConverter {
}
DocPath p = relativePath.resolve(stylesheet);
HtmlTree link = HtmlTree.LINK("stylesheet", "text/css", p.getPath(), "Style");
return link;
head.addContent(link);
addStylesheets(head);
}
protected void addStylesheets(Content tree) {
List<String> stylesheets = configuration.additionalStylesheets;
if (!stylesheets.isEmpty()) {
stylesheets.forEach((ssheet) -> {
DocFile file = DocFile.createFileForInput(configuration, ssheet);
DocPath ssheetPath = DocPath.create(file.getName());
HtmlTree slink = HtmlTree.LINK("stylesheet", "text/css", relativePath.resolve(ssheetPath).getPath(),
"Style");
tree.addContent(slink);
});
}
}
/**

View File

@ -311,9 +311,9 @@ public abstract class HtmlDocWriter extends HtmlWriter {
* Returns a link to the stylesheet file.
*
* @param configuration the configuration for this doclet
* @return an HtmlTree for the lINK tag which provides the stylesheet location
* @param head HtmlTree to which the stylesheet links will be added
*/
public HtmlTree getStyleSheetProperties(HtmlConfiguration configuration) {
public void addStyleSheetProperties(HtmlConfiguration configuration, Content head) {
String stylesheetfile = configuration.stylesheetfile;
DocPath stylesheet;
if (stylesheetfile.isEmpty()) {
@ -325,7 +325,21 @@ public abstract class HtmlDocWriter extends HtmlWriter {
HtmlTree link = HtmlTree.LINK("stylesheet", "text/css",
pathToRoot.resolve(stylesheet).getPath(),
"Style");
return link;
head.addContent(link);
addStylesheets(configuration, head);
}
protected void addStylesheets(HtmlConfiguration configuration, Content tree) {
List<String> stylesheets = configuration.additionalStylesheets;
if (!stylesheets.isEmpty()) {
stylesheets.forEach((ssheet) -> {
DocFile file = DocFile.createFileForInput(configuration, ssheet);
DocPath ssheetPath = DocPath.create(file.getName());
HtmlTree slink = HtmlTree.LINK("stylesheet", "text/css", pathToRoot.resolve(ssheetPath).getPath(),
"Style");
tree.addContent(slink);
});
}
}
protected Comment getGeneratedBy(boolean timestamp) {

View File

@ -175,6 +175,10 @@ doclet.Groupname_already_used=In -group option, group name already used: {0}
doclet.Same_element_name_used=Element name or pattern used twice: {0}
# option specifiers
doclet.usage.add-stylesheet.parameters=\
<file>
doclet.usage.add-stylesheet.description=\
Additional stylesheet file for the generated documentation
doclet.usage.d.parameters=\
<directory>
doclet.usage.d.description=\
@ -329,9 +333,9 @@ doclet.usage.sourcetab.description=\
doclet.usage.keywords.description=\
Include HTML meta tags with package, class and member info
doclet.usage.stylesheetfile.parameters=\
<path>
doclet.usage.stylesheetfile.description=\
doclet.usage.main-stylesheet.parameters=\
<file>
doclet.usage.main-stylesheet.description=\
File to change style of the generated documentation
doclet.usage.docencoding.parameters=\

View File

@ -23,7 +23,7 @@
/*
* @test
* @bug 4934778 4777599 6553182 8146427 8146475 8175055
* @bug 4934778 4777599 6553182 8146427 8146475 8175055 8185371
* @summary Make sure that -help, -helpfile and -nohelp options work correctly.
* @author jamieh
* @library ../lib
@ -161,6 +161,7 @@ public class TestHelpOption extends JavadocTester {
"-sourcetab ",
"-keywords ",
"-stylesheetfile ",
"--add-stylesheet ",
"-docencoding ",
"-html4 ",
"-html5 ",

View File

@ -23,8 +23,9 @@
/*
* @test
* @bug 4749567 8071982 8175200 8186332
* @summary Test the output for -header, -footer, -nooverview, -nodeprecatedlist, -nonavbar, -notree, -stylesheetfile options.
* @bug 4749567 8071982 8175200 8186332 8185371
* @summary Test the output for -header, -footer, -nooverview, -nodeprecatedlist, -nonavbar, -notree,
* -stylesheetfile, --main-stylesheet, --add-stylesheet options.
* @author Bhavesh Patel
* @library ../lib
* @modules jdk.javadoc/jdk.javadoc.internal.tool
@ -117,6 +118,64 @@ public class TestOptions extends JavadocTester {
+ "href=\"../custom-stylesheet.css\" title=\"Style\">");
}
@Test
void testStylesheetFileAltOption() {
javadoc("-d", "out-stylesheet-file",
"--main-stylesheet", new File(testSrc, "custom-stylesheet.css").getAbsolutePath(),
"-sourcepath", testSrc,
"pkg");
checkExit(Exit.OK);
checkOutput("custom-stylesheet.css", true, "Custom javadoc style sheet");
checkOutput("pkg/Foo.html", true, "<link rel=\"stylesheet\" type=\"text/css\" "
+ "href=\"../custom-stylesheet.css\" title=\"Style\">");
}
@Test
void testAdditionalStylesheetFile() {
javadoc("-d", "out-additional-css",
"--add-stylesheet", new File(testSrc, "additional-stylesheet-1.css").getAbsolutePath(),
"--add-stylesheet", new File(testSrc, "additional-stylesheet-2.css").getAbsolutePath(),
"--add-stylesheet", new File(testSrc, "additional-stylesheet-3.css").getAbsolutePath(),
"-sourcepath", testSrc,
"pkg");
checkExit(Exit.OK);
checkOutput("additional-stylesheet-1.css", true, "Additional javadoc style sheet 1");
checkOutput("additional-stylesheet-2.css", true, "Additional javadoc style sheet 2");
checkOutput("additional-stylesheet-3.css", true, "Additional javadoc style sheet 3");
checkOutput("pkg/Foo.html", true,
"<link rel=\"stylesheet\" type=\"text/css\" href=\"../additional-stylesheet-1.css\" title=\"Style\">\n"
+ "<link rel=\"stylesheet\" type=\"text/css\" href=\"../additional-stylesheet-2.css\" title=\"Style\">\n"
+ "<link rel=\"stylesheet\" type=\"text/css\" href=\"../additional-stylesheet-3.css\" title=\"Style\">");
}
@Test
void testInvalidStylesheetFile() {
javadoc("-d", "out-invalid-css",
"--main-stylesheet", new File(testSrc, "custom-stylesheet-1.css").getAbsolutePath(),
"-sourcepath", testSrc,
"pkg");
checkExit(Exit.ERROR);
checkOutput(Output.OUT, true,
"javadoc: error - File not found:",
"custom-stylesheet-1.css");
}
@Test
void testInvalidAdditionalStylesheetFiles() {
javadoc("-d", "out-invalid-additional-css",
"--add-stylesheet", new File(testSrc, "additional-stylesheet-4.css").getAbsolutePath(),
"-sourcepath", testSrc,
"pkg");
checkExit(Exit.ERROR);
checkOutput(Output.OUT, true,
"javadoc: error - File not found:",
"additional-stylesheet-4.css");
}
@Test
void testLinkSource() {
javadoc("-d", "out-9",

View File

@ -0,0 +1,5 @@
/* Additional javadoc style sheet 1 */
body {
background-color:#f8f8ff;
}

View File

@ -0,0 +1,5 @@
/* Additional javadoc style sheet 2 */
.subNav {
background-color:#fafad2;
}

View File

@ -0,0 +1,5 @@
/* Additional javadoc style sheet 3 */
a:link, a:visited {
color:#8b0000;
}