# HG changeset patch # User ksrini # Date 1516159608 28800 # Node ID f94706337b07380e1c4e68733c02020cdcccf652 # Parent 9cf44c40aa35fc54e724fa9ba3545225c858e64b 8194953: doclet corrupts HTML files when adding navbar Reviewed-by: jjg diff -r 9cf44c40aa35 -r f94706337b07 src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/DocFilesHandlerImpl.java --- a/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/DocFilesHandlerImpl.java Tue Jan 16 17:27:06 2018 -0800 +++ b/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/DocFilesHandlerImpl.java Tue Jan 16 19:26:48 2018 -0800 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2018, 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 @@ -25,10 +25,17 @@ package jdk.javadoc.internal.doclets.formats.html; +import com.sun.source.doctree.AttributeTree; +import com.sun.source.doctree.AttributeTree.ValueKind; +import com.sun.source.doctree.DocRootTree; import com.sun.source.doctree.DocTree; import com.sun.source.doctree.EndElementTree; +import com.sun.source.doctree.LinkTree; import com.sun.source.doctree.StartElementTree; import com.sun.source.doctree.TextTree; +import com.sun.source.util.SimpleDocTreeVisitor; +import com.sun.tools.doclint.HtmlTag; +import com.sun.tools.doclint.HtmlTag.Attr; import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree; import jdk.javadoc.internal.doclets.toolkit.Content; import jdk.javadoc.internal.doclets.toolkit.DocFileElement; @@ -123,13 +130,14 @@ configuration.messages.warning("doclet.Copy_Overwrite_warning", srcfile.getPath(), dstdir.getPath()); } else { + if (Utils.toLowerCase(srcfile.getPath()).endsWith(".html")) { + if (handleHtmlFile(srcfile, dstDocPath)) { + continue; + } + } configuration.messages.notice("doclet.Copying_File_0_To_Dir_1", srcfile.getPath(), dstdir.getPath()); - if (Utils.toLowerCase(srcfile.getPath()).endsWith(".html")) { - handleHtmlFile(srcfile, dstDocPath); - } else { - destfile.copyFile(srcfile); - } + destfile.copyFile(srcfile); } } else if (srcfile.isDirectory()) { if (configuration.copydocfilesubdirs @@ -141,19 +149,23 @@ } } - private void handleHtmlFile(DocFile srcfile, DocPath dstPath) throws DocFileIOException { + private boolean 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); - - Utils utils = configuration.utils; + configuration.messages.notice("doclet.Generating_0", docletWriter.filename); - FileObject fileObject = srcfile.getFileObject(); - DocFileElement dfElement = new DocFileElement(element, fileObject); String title = getWindowTitle(docletWriter, dfElement).trim(); HtmlTree htmlContent = docletWriter.getBody(true, title); docletWriter.addTop(htmlContent); docletWriter.addNavLinks(true, htmlContent); - List fullBody = utils.getFullBody(dfElement); Content bodyContent = docletWriter.commentTagsToContent(null, dfElement, fullBody, false); @@ -163,6 +175,69 @@ docletWriter.addNavLinks(false, htmlContent); docletWriter.addBottom(htmlContent); docletWriter.printHtmlDocument(Collections.emptyList(), false, htmlContent); + return true; + } + + + private boolean shouldPassThrough(List dtrees) { + SimpleDocTreeVisitor check = new SimpleDocTreeVisitor() { + @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() { + + @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; + } + }; + for (DocTree dt : dtrees) { + if (check.visit(dt, false)) + return true; + } + return false; } private String getWindowTitle(HtmlDocletWriter docletWriter, Element element) { diff -r 9cf44c40aa35 -r f94706337b07 test/langtools/jdk/javadoc/doclet/testCopyFiles/TestCopyFiles.java --- a/test/langtools/jdk/javadoc/doclet/testCopyFiles/TestCopyFiles.java Tue Jan 16 17:27:06 2018 -0800 +++ b/test/langtools/jdk/javadoc/doclet/testCopyFiles/TestCopyFiles.java Tue Jan 16 19:26:48 2018 -0800 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2016, 2018, 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 + * @bug 8157349 8185985 8194953 * @summary test copy of doc-files, and its contents for HTML meta content. * @library ../lib * @modules jdk.javadoc/jdk.javadoc.internal.tool @@ -258,4 +258,16 @@ "A named package in an unnamed module" ); } + + @Test + void testCopyThrough() { + javadoc("-d", "copy", + "-sourcepath", testSrc("packages"), + "p2"); + checkExit(Exit.OK); + checkOutput("p2/doc-files/case1.html", true, " + + + + + Case 1 + + + This should be translated with a navbar, and should have + the "Generated by javadoc blah" signature. + + diff -r 9cf44c40aa35 -r f94706337b07 test/langtools/jdk/javadoc/doclet/testCopyFiles/packages/p2/doc-files/case2.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/langtools/jdk/javadoc/doclet/testCopyFiles/packages/p2/doc-files/case2.html Tue Jan 16 19:26:48 2018 -0800 @@ -0,0 +1,44 @@ + + + + + + + + Case 2 + + + + This should be copied directly, with no modifications. + + diff -r 9cf44c40aa35 -r f94706337b07 test/langtools/jdk/javadoc/doclet/testCopyFiles/packages/p2/doc-files/case3.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/langtools/jdk/javadoc/doclet/testCopyFiles/packages/p2/doc-files/case3.html Tue Jan 16 19:26:48 2018 -0800 @@ -0,0 +1,42 @@ + + + + + + + + Case 3 + + + + This should be copied directly, with no modifications. + + diff -r 9cf44c40aa35 -r f94706337b07 test/langtools/jdk/javadoc/doclet/testCopyFiles/packages/p2/doc-files/case4.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/langtools/jdk/javadoc/doclet/testCopyFiles/packages/p2/doc-files/case4.html Tue Jan 16 19:26:48 2018 -0800 @@ -0,0 +1,39 @@ + + + + + + + + Case 3 + + + + This should be copied directly, with no modifications. + +