8183582: Rationalize doclet -docencoding and -charset options
authorpmuthuswamy
Wed, 02 Aug 2017 09:26:35 -0700
changeset 46080 65ccd412049b
parent 46079 059faa5e1267
child 46081 7c6d73d10b6b
8183582: Rationalize doclet -docencoding and -charset options Reviewed-by: jjg, ksrini
langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlConfiguration.java
langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlDocletWriter.java
langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/IndexRedirectWriter.java
langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/markup/HtmlDocWriter.java
langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/BaseConfiguration.java
langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/StandardDocFileFactory.java
langtools/test/jdk/javadoc/doclet/testCharsetDocencodingOptions/TestCharsetDocencodingOptions.java
langtools/test/jdk/javadoc/doclet/testCharsetDocencodingOptions/pkg/Foo.java
--- a/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlConfiguration.java	Fri Jul 28 15:00:53 2017 -0700
+++ b/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlConfiguration.java	Wed Aug 02 09:26:35 2017 -0700
@@ -40,10 +40,12 @@
 
 import jdk.javadoc.doclet.Doclet;
 import jdk.javadoc.doclet.DocletEnvironment;
+import jdk.javadoc.internal.doclets.formats.html.markup.HtmlConstants;
 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTag;
 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlVersion;
 import jdk.javadoc.internal.doclets.toolkit.BaseConfiguration;
 import jdk.javadoc.internal.doclets.toolkit.Content;
+import jdk.javadoc.internal.doclets.toolkit.DocletException;
 import jdk.javadoc.internal.doclets.toolkit.Messages;
 import jdk.javadoc.internal.doclets.toolkit.Resources;
 import jdk.javadoc.internal.doclets.toolkit.WriterFactory;
@@ -162,6 +164,11 @@
     public boolean createtree = true;
 
     /**
+     * The META charset tag used for cross-platform viewing.
+     */
+    public String charset = null;
+
+    /**
      * True if command line option "-nodeprecated" is used. Default value is
      * false.
      */
@@ -797,4 +804,23 @@
         oset.addAll(super.getSupportedOptions());
         return oset;
     }
+
+    @Override
+    protected boolean finishOptionSettings0() throws DocletException {
+        if (docencoding == null) {
+            if (charset == null) {
+                docencoding = charset = (encoding == null) ? HtmlConstants.HTML_DEFAULT_CHARSET : encoding;
+            } else {
+                docencoding = charset;
+            }
+        } else {
+            if (charset == null) {
+                charset = docencoding;
+            } else if (!charset.equals(docencoding)) {
+                reporter.print(ERROR, getText("doclet.Option_conflict", "-charset", "-docencoding"));
+                return false;
+            }
+        }
+        return super.finishOptionSettings0();
+    }
 }
--- a/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlDocletWriter.java	Fri Jul 28 15:00:53 2017 -0700
+++ b/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlDocletWriter.java	Wed Aug 02 09:26:35 2017 -0700
@@ -446,9 +446,7 @@
         Content head = new HtmlTree(HtmlTag.HEAD);
         head.addContent(getGeneratedBy(!configuration.notimestamp));
         head.addContent(getTitle());
-        Content meta = HtmlTree.META("Content-Type", CONTENT_TYPE,
-                (configuration.charset.length() > 0) ?
-                        configuration.charset : HtmlConstants.HTML_DEFAULT_CHARSET);
+        Content meta = HtmlTree.META("Content-Type", CONTENT_TYPE, configuration.charset);
         head.addContent(meta);
         if (!configuration.notimestamp) {
             SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
--- a/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/IndexRedirectWriter.java	Fri Jul 28 15:00:53 2017 -0700
+++ b/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/IndexRedirectWriter.java	Wed Aug 02 09:26:35 2017 -0700
@@ -78,9 +78,7 @@
 
         Content windowTitle = HtmlTree.TITLE(new StringContent(title));
         head.addContent(windowTitle);
-        Content metaContentType = HtmlTree.META("Content", CONTENT_TYPE,
-                (configuration.charset.length() > 0) ?
-                        configuration.charset : HtmlConstants.HTML_DEFAULT_CHARSET);
+        Content metaContentType = HtmlTree.META("Content", CONTENT_TYPE, configuration.charset);
         head.addContent(metaContentType);
 
         String topFilePath = configuration.topFile.getPath();
--- a/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/markup/HtmlDocWriter.java	Fri Jul 28 15:00:53 2017 -0700
+++ b/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/markup/HtmlDocWriter.java	Wed Aug 02 09:26:35 2017 -0700
@@ -328,9 +328,7 @@
         head.addContent(getGeneratedBy(!configuration.notimestamp));
         Content windowTitle = HtmlTree.TITLE(new StringContent(title));
         head.addContent(windowTitle);
-        Content meta = HtmlTree.META("Content-Type", CONTENT_TYPE,
-                (configuration.charset.length() > 0) ?
-                        configuration.charset : HtmlConstants.HTML_DEFAULT_CHARSET);
+        Content meta = HtmlTree.META("Content-Type", CONTENT_TYPE, configuration.charset);
         head.addContent(meta);
         head.addContent(getStyleSheetProperties(configuration));
         head.addContent(getFramesJavaScript());
--- a/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/BaseConfiguration.java	Fri Jul 28 15:00:53 2017 -0700
+++ b/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/BaseConfiguration.java	Wed Aug 02 09:26:35 2017 -0700
@@ -143,11 +143,6 @@
     public boolean backwardCompatibility = true;
 
     /**
-     * The META charset tag used for cross-platform viewing.
-     */
-    public String charset = "";
-
-    /**
      * True if user wants to add member names as meta keywords.
      * Set to false because meta keywords are ignored in general
      * by most Internet search engines.
@@ -693,7 +688,7 @@
      * when this is called all the option have been set, this method,
      * initializes certain components before anything else is started.
      */
-    private void finishOptionSettings0() throws DocletException {
+    protected boolean finishOptionSettings0() throws DocletException {
         initDestDirectory();
         for (String link : linkList) {
             extern.link(link, reporter);
@@ -701,9 +696,6 @@
         for (Pair<String, String> linkOfflinePair : linkOfflineList) {
             extern.link(linkOfflinePair.first, linkOfflinePair.second, reporter);
         }
-        if (docencoding == null) {
-            docencoding = encoding;
-        }
         typeElementCatalog = new TypeElementCatalog(includedTypeElements, this);
         initTagletManager(customTagStrs);
         groupPairs.stream().forEach((grp) -> {
@@ -713,6 +705,7 @@
                 group.checkPackageGroups(grp.first, grp.second);
             }
         });
+        return true;
     }
 
     /**
@@ -724,8 +717,7 @@
     public boolean setOptions() throws DocletException {
         initPackages();
         initModules();
-        finishOptionSettings0();
-        if (!finishOptionSettings())
+        if (!finishOptionSettings0() || !finishOptionSettings())
             return false;
 
         return true;
--- a/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/StandardDocFileFactory.java	Fri Jul 28 15:00:53 2017 -0700
+++ b/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/StandardDocFileFactory.java	Wed Aug 02 09:26:35 2017 -0700
@@ -202,11 +202,7 @@
 
             try {
                 OutputStream out = getFileObjectForOutput(path).openOutputStream();
-                if (configuration.docencoding == null) {
-                    return new BufferedWriter(new OutputStreamWriter(out));
-                } else {
-                    return new BufferedWriter(new OutputStreamWriter(out, configuration.docencoding));
-                }
+                return new BufferedWriter(new OutputStreamWriter(out, configuration.docencoding));
             } catch (IOException e) {
                 throw new DocFileIOException(this, DocFileIOException.Mode.WRITE, e);
             }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/jdk/javadoc/doclet/testCharsetDocencodingOptions/TestCharsetDocencodingOptions.java	Wed Aug 02 09:26:35 2017 -0700
@@ -0,0 +1,116 @@
+/*
+ * 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.
+ */
+
+/*
+ * @test
+ * @bug      8183582
+ * @summary  Rationalize doclet -docencoding and -charset options.
+ * @library  ../lib
+ * @modules jdk.javadoc/jdk.javadoc.internal.tool
+ * @build    JavadocTester
+ * @run main TestCharsetDocencodingOptions
+ */
+
+public class TestCharsetDocencodingOptions extends JavadocTester {
+
+    public static void main(String... args) throws Exception {
+        TestCharsetDocencodingOptions tester = new TestCharsetDocencodingOptions();
+        tester.runTests();
+    }
+
+    @Test
+    void testWithNoOptions() {
+        javadoc("-d", "out",
+                "-sourcepath", testSrc,
+                "pkg");
+        checkExit(Exit.OK);
+
+        checkOutputFileEncoding("utf-8");
+    }
+
+    @Test
+    void testWithDocencoding() {
+        javadoc("-d", "out-1",
+                "-docencoding", "ISO-8859-1",
+                "-sourcepath", testSrc,
+                "pkg");
+        checkExit(Exit.OK);
+
+        checkOutputFileEncoding("ISO-8859-1");
+    }
+
+    @Test
+    void testWithCharset() {
+        javadoc("-d", "out-2",
+                "-charset", "ISO-8859-1",
+                "-sourcepath", testSrc,
+                "pkg");
+        checkExit(Exit.OK);
+
+        checkOutputFileEncoding("ISO-8859-1");
+    }
+
+    @Test
+    void testDocencodingWithCharsetSimilar() {
+        javadoc("-d", "out-3",
+                "-docencoding", "ISO-8859-1",
+                "-charset", "ISO-8859-1",
+                "-sourcepath", testSrc,
+                "pkg");
+        checkExit(Exit.OK);
+
+        checkOutputFileEncoding("ISO-8859-1");
+    }
+
+    @Test
+    void testDocencodingWithCharsetDifferent() {
+        javadoc("-d", "out-4",
+                "-charset", "UTF-8",
+                "-docencoding", "ISO-8859-1",
+                "-sourcepath", testSrc,
+                "pkg");
+        checkExit(Exit.ERROR);
+
+        checkOutput(Output.OUT, true,
+                "javadoc: error - Option -charset conflicts with -docencoding");
+    }
+
+    @Test
+    void testWithEncoding() {
+        javadoc("-d", "out-5",
+                "-sourcepath", testSrc,
+                "-encoding", "ISO-8859-1",
+                "pkg");
+        checkExit(Exit.OK);
+
+        checkOutputFileEncoding("ISO-8859-1");
+    }
+
+
+    void checkOutputFileEncoding(String charset) {
+        checkOutput("index.html", true,
+                "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=" + charset + "\">");
+        checkOutput("pkg/Foo.html", true,
+                "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=" + charset + "\">");
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/jdk/javadoc/doclet/testCharsetDocencodingOptions/pkg/Foo.java	Wed Aug 02 09:26:35 2017 -0700
@@ -0,0 +1,26 @@
+/*
+ * 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 pkg;
+
+public class Foo {}