changeset 45860:0952e2c6545a

8179704: doclet crashes with an empty package.html Reviewed-by: jjg
author ksrini
date Wed, 05 Jul 2017 13:46:05 -0700
parents 9ef5029b247b
children a82ccda077c9
files langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlDocletWriter.java langtools/test/jdk/javadoc/doclet/testPackageHtml/TestPackageHtml.java langtools/test/jdk/javadoc/doclet/testPackageHtml/pkg2/X.java langtools/test/jdk/javadoc/doclet/testPackageHtml/pkg2/package.html langtools/test/jdk/javadoc/doclet/testPackageHtml/pkg3/X.java langtools/test/jdk/javadoc/doclet/testPackageHtml/pkg3/package.html langtools/test/jdk/javadoc/doclet/testPackageHtml/pkg4/X.java langtools/test/jdk/javadoc/doclet/testPackageHtml/pkg4/package.html
diffstat 8 files changed, 167 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlDocletWriter.java	Thu Aug 24 16:25:08 2017 +0200
+++ b/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlDocletWriter.java	Wed Jul 05 13:46:05 2017 -0700
@@ -1757,6 +1757,9 @@
         return true;
     }
 
+    // Notify the next DocTree handler to take necessary action
+    private boolean commentRemoved = false;
+
     /**
      * Converts inline tags and text to text strings, expanding the
      * inline tags along the way.  Called wherever text can contain
@@ -1782,27 +1785,32 @@
         CommentHelper ch = utils.getCommentHelper(element);
         // Array of all possible inline tags for this javadoc run
         configuration.tagletManager.checkTags(utils, element, tags, true);
+        commentRemoved = false;
+
         for (ListIterator<? extends DocTree> iterator = tags.listIterator(); iterator.hasNext();) {
+            boolean isFirstNode = !iterator.hasPrevious();
             DocTree tag = iterator.next();
-             // zap block tags
-            if (isFirstSentence && ignoreNonInlineTag(tag))
-                continue;
+            boolean isLastNode  = !iterator.hasNext();
 
-            if (isFirstSentence && iterator.nextIndex() == tags.size() &&
-                    (tag.getKind() == TEXT && isAllWhiteSpace(ch.getText(tag))))
-                continue;
+            if (isFirstSentence) {
+                // Ignore block tags
+                if (ignoreNonInlineTag(tag))
+                    continue;
+
+                // Ignore any trailing whitespace OR whitespace after removed html comment
+                if ((isLastNode || commentRemoved)
+                        && tag.getKind() == TEXT
+                        && isAllWhiteSpace(ch.getText(tag)))
+                    continue;
+
+                // Ignore any leading html comments
+                if ((isFirstNode || commentRemoved) && tag.getKind() == COMMENT) {
+                    commentRemoved = true;
+                    continue;
+                }
+            }
 
             boolean allDone = new SimpleDocTreeVisitor<Boolean, Content>() {
-                // notify the next DocTree handler to take necessary action
-                boolean commentRemoved = false;
-
-                private boolean isLast(DocTree node) {
-                    return node.equals(tags.get(tags.size() - 1));
-                }
-
-                private boolean isFirst(DocTree node) {
-                    return node.equals(tags.get(0));
-                }
 
                 private boolean inAnAtag() {
                     if (utils.isStartElement(tag)) {
@@ -1847,14 +1855,14 @@
                             if (text.startsWith("/..") && !configuration.docrootparent.isEmpty()) {
                                 result.addContent(configuration.docrootparent);
                                 docRootContent = new ContentBuilder();
-                                result.addContent(textCleanup(text.substring(3), isLast(node)));
+                                result.addContent(textCleanup(text.substring(3), isLastNode));
                             } else {
                                 if (!docRootContent.isEmpty()) {
                                     docRootContent = copyDocRootContent(docRootContent);
                                 } else {
                                     text = redirectRelativeLinks(element, (TextTree) dt);
                                 }
-                                result.addContent(textCleanup(text, isLast(node)));
+                                result.addContent(textCleanup(text, isLastNode));
                             }
                         } else {
                             docRootContent = copyDocRootContent(docRootContent);
@@ -1868,10 +1876,6 @@
 
                 @Override
                 public Boolean visitComment(CommentTree node, Content c) {
-                    if (isFirstSentence && isFirst(node)) {
-                        commentRemoved = true;
-                        return this.visit(iterator.next(), c);
-                    }
                     result.addContent(new RawHtml(node.getBody()));
                     return false;
                 }
@@ -1996,8 +2000,7 @@
                 @Override
                 public Boolean visitText(TextTree node, Content c) {
                     String text = node.getBody();
-                    result.addContent(new RawHtml(textCleanup(text, isLast(node), commentRemoved)));
-                    commentRemoved = false;
+                    result.addContent(new RawHtml(textCleanup(text, isLastNode, commentRemoved)));
                     return false;
                 }
 
@@ -2013,6 +2016,7 @@
                 }
 
             }.visit(tag, null);
+            commentRemoved = false;
             if (allDone)
                 break;
         }
--- a/langtools/test/jdk/javadoc/doclet/testPackageHtml/TestPackageHtml.java	Thu Aug 24 16:25:08 2017 +0200
+++ b/langtools/test/jdk/javadoc/doclet/testPackageHtml/TestPackageHtml.java	Wed Jul 05 13:46:05 2017 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2017, 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,8 +23,8 @@
 
 /*
  * @test
- * @bug      8150096
- * @summary  Make sure package.html is recognized by doclint
+ * @bug      8150096 8179704
+ * @summary  test package.html handling
  * @library  ../lib
  * @modules jdk.javadoc/jdk.javadoc.internal.tool
  * @build    JavadocTester
@@ -37,12 +37,36 @@
         tester.runTests();
     }
 
+    // Make sure package.html is recognized by doclint
     @Test
     void testPackageHtml() {
-        javadoc("-d", "out-pkg-html",
+        javadoc("-d", "out-pkg-html-1",
                 "-sourcepath", testSrc,
                 "pkg1");
         checkExit(Exit.ERROR);
         checkOutput(Output.OUT, true, "package.html:10: error: bad use of '>'");
     }
+
+    // Doclet must handle empty body in package.html, must
+    // ignore html comment in the first sentence and must
+    // ignore trailing whitespace in a first sentence.
+    @Test
+    void testPackageHtmlWithEmptyBody() {
+        javadoc("-d", "out-pkg-html-2",
+                "-sourcepath", testSrc,
+                "pkg2", "pkg3", "pkg4");
+        checkExit(Exit.OK);
+        checkOutput("index-all.html", true,
+              "<dl>\n"
+                      + "<dt><a href=\"pkg2/package-summary.html\">pkg2</a> - package pkg2</dt>\n"
+                      + "<dt><a href=\"pkg3/package-summary.html\">pkg3</a> - package pkg3</dt>\n"
+                      + "<dd>\n"
+                      + "<div class=\"block\">This is a documentation for <a href=\"pkg3/package-summary.html\"><code>pkg3</code></a></div>\n"
+                      + "</dd>\n"
+                      + "<dt><a href=\"pkg4/package-summary.html\">pkg4</a> - package pkg4</dt>\n"
+                      + "<dd>\n"
+                      + "<div class=\"block\">This is a documentation for <a href=\"pkg4/package-summary.html\"><code>pkg4</code></a></div>\n"
+                      + "</dd>\n"
+                      + "</dl>\n");
+    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/jdk/javadoc/doclet/testPackageHtml/pkg2/X.java	Wed Jul 05 13:46:05 2017 -0700
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2017, 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.
+ *
+ * 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.
+ */
+
+package pkg2;
+
+/**
+ * An empty class.
+ */
+public class X {}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/jdk/javadoc/doclet/testPackageHtml/pkg2/package.html	Wed Jul 05 13:46:05 2017 -0700
@@ -0,0 +1,6 @@
+<HTML>
+    <BODY>
+        <!-- an empty package.html -->
+    </BODY>
+</HTML>
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/jdk/javadoc/doclet/testPackageHtml/pkg3/X.java	Wed Jul 05 13:46:05 2017 -0700
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2017, 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.
+ *
+ * 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.
+ */
+
+package pkg3;
+
+/**
+ * An empty class.
+ */
+public class X {}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/jdk/javadoc/doclet/testPackageHtml/pkg3/package.html	Wed Jul 05 13:46:05 2017 -0700
@@ -0,0 +1,9 @@
+<HTML>
+    <BODY>
+        <!-- a HTML comment -->
+        This is a documentation for {@link pkg3}
+
+    <p>Next para</p>
+    </BODY>
+</HTML>
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/jdk/javadoc/doclet/testPackageHtml/pkg4/X.java	Wed Jul 05 13:46:05 2017 -0700
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2017, 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.
+ *
+ * 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.
+ */
+
+package pkg4;
+
+/**
+ * An empty class.
+ */
+public class X {}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/jdk/javadoc/doclet/testPackageHtml/pkg4/package.html	Wed Jul 05 13:46:05 2017 -0700
@@ -0,0 +1,9 @@
+<HTML>
+    <BODY>
+        <!-- a HTML comment -->    <!--another comment -->
+        This is a documentation for {@link pkg4}
+
+    <p>Next para</p>
+    </BODY>
+</HTML>
+