8194953: doclet corrupts HTML files when adding navbar
authorksrini
Tue, 16 Jan 2018 19:26:48 -0800
changeset 48552 f94706337b07
parent 48551 9cf44c40aa35
child 48553 fe2950b07f1e
child 48591 ca245f9f70db
8194953: doclet corrupts HTML files when adding navbar Reviewed-by: jjg
src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/DocFilesHandlerImpl.java
test/langtools/jdk/javadoc/doclet/testCopyFiles/TestCopyFiles.java
test/langtools/jdk/javadoc/doclet/testCopyFiles/packages/p2/Foo.java
test/langtools/jdk/javadoc/doclet/testCopyFiles/packages/p2/doc-files/case1.html
test/langtools/jdk/javadoc/doclet/testCopyFiles/packages/p2/doc-files/case2.html
test/langtools/jdk/javadoc/doclet/testCopyFiles/packages/p2/doc-files/case3.html
test/langtools/jdk/javadoc/doclet/testCopyFiles/packages/p2/doc-files/case4.html
--- 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<? extends DocTree> 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<? 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;
+            }
+        };
+        for (DocTree dt : dtrees) {
+            if (check.visit(dt, false))
+                return true;
+        }
+        return false;
     }
 
     private String getWindowTitle(HtmlDocletWriter docletWriter, Element element) {
--- 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, "<!-- 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");
+    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/jdk/javadoc/doclet/testCopyFiles/packages/p2/Foo.java	Tue Jan 16 19:26:48 2018 -0800
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+ /**
+  * A test class.
+  */
+package p2;
+
+/**
+ * A test class.
+ */
+public class Foo {}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/jdk/javadoc/doclet/testCopyFiles/packages/p2/doc-files/case1.html	Tue Jan 16 19:26:48 2018 -0800
@@ -0,0 +1,39 @@
+<!doctype html>
+
+<!--
+/*
+ * Copyright (c) 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+-->
+
+<html lang="en">
+  <head>
+    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
+    <title>Case 1</title>
+  </head>
+  <body>
+     This should be translated with a navbar, and should have
+     the "Generated by javadoc blah" signature.
+  </body>
+</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 @@
+<!doctype html>
+
+<!--
+/*
+ * Copyright (c) 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+-->
+
+<html lang="en">
+  <head>
+    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
+    <title>Case 2</title>
+    <style type="text/css">
+      body {
+        font-family: Helvetica, Arial, sans-serif;
+        font-size: 14px;
+      }
+    </style>
+  </head>
+  <body>
+  This should be copied directly, with no modifications.
+  </body>
+</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 @@
+<!doctype html>
+
+<!--
+/*
+ * Copyright (c) 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+-->
+
+<html lang="en">
+  <head>
+    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
+    <title>Case 3</title>
+      <style>
+        h1 {color:red;}
+        p {color:blue;}
+      </style>
+  </head>
+  <body>
+  This should be copied directly, with no modifications.
+  </body>
+</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 @@
+<!doctype html>
+
+<!--
+/*
+ * Copyright (c) 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+-->
+
+<html lang="en">
+  <head>
+    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
+    <title>Case 3</title>
+    <link rel="stylesheet" type="text/css" href="theme.css"> 
+  </head>
+  <body>
+  This should be copied directly, with no modifications.
+  </body>
+</html>