8149565: -locale option issues
authorjjg
Mon, 18 Jun 2018 13:40:16 -0700
changeset 50611 69c59b667acc
parent 50610 9b85066e259b
child 50612 e10812a82a3b
8149565: -locale option issues Reviewed-by: hannesw
src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/Start.java
src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/resources/javadoc.properties
test/langtools/ProblemList.txt
test/langtools/jdk/javadoc/tool/VerifyLocale.java
--- a/src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/Start.java	Mon Jun 18 10:13:58 2018 -0700
+++ b/src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/Start.java	Mon Jun 18 13:40:16 2018 -0700
@@ -36,6 +36,7 @@
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
+import java.util.IllformedLocaleException;
 import java.util.List;
 import java.util.Locale;
 import java.util.MissingResourceException;
@@ -853,46 +854,17 @@
      * then return default locale.
      */
     private Locale getLocale(String localeName) throws ToolException {
-        Locale userlocale = null;
-        if (localeName == null || localeName.isEmpty()) {
-            return Locale.getDefault();
-        }
-        int firstuscore = localeName.indexOf('_');
-        int seconduscore = -1;
-        String language = null;
-        String country = null;
-        String variant = null;
-        if (firstuscore == 2) {
-            language = localeName.substring(0, firstuscore);
-            seconduscore = localeName.indexOf('_', firstuscore + 1);
-            if (seconduscore > 0) {
-                if (seconduscore != firstuscore + 3
-                        || localeName.length() <= seconduscore + 1) {
-                    String text = messager.getText("main.malformed_locale_name", localeName);
-                    throw new ToolException(CMDERR, text);
-                }
-                country = localeName.substring(firstuscore + 1,
-                        seconduscore);
-                variant = localeName.substring(seconduscore + 1);
-            } else if (localeName.length() == firstuscore + 3) {
-                country = localeName.substring(firstuscore + 1);
-            } else {
-                String text = messager.getText("main.malformed_locale_name", localeName);
-                throw new ToolException(CMDERR, text);
-            }
-        } else if (firstuscore == -1 && localeName.length() == 2) {
-            language = localeName;
-        } else {
+        try {
+            // Tolerate, at least for a while, the older syntax accepted by javadoc,
+            // using _ as the separator
+            localeName = localeName.replace("_", "-");
+            Locale l =  new Locale.Builder().setLanguageTag(localeName).build();
+            // Ensure that a non-empty language is available for the <HTML lang=...> element
+            return (l.getLanguage().isEmpty()) ? Locale.ENGLISH : l;
+        } catch (IllformedLocaleException e) {
             String text = messager.getText("main.malformed_locale_name", localeName);
             throw new ToolException(CMDERR, text);
         }
-        userlocale = searchLocale(language, country, variant);
-        if (userlocale == null) {
-            String text = messager.getText("main.illegal_locale_name", localeName);
-            throw new ToolException(CMDERR, text);
-        } else {
-            return userlocale;
-        }
     }
 
     /**
--- a/src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/resources/javadoc.properties	Mon Jun 18 10:13:58 2018 -0700
+++ b/src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/resources/javadoc.properties	Mon Jun 18 13:40:16 2018 -0700
@@ -285,7 +285,6 @@
 main.doclet_no_classloader_found=Could not obtain classloader to load {0}
 main.could_not_instantiate_class=Could not instantiate class {0}
 main.doclet_class_not_found=Cannot find doclet class {0}
-main.illegal_locale_name=Locale not available: {0}
 main.malformed_locale_name=Malformed locale name: {0}
 main.file_not_found=File not found: "{0}"
 main.illegal_class_name=Illegal class name: "{0}"
--- a/test/langtools/ProblemList.txt	Mon Jun 18 10:13:58 2018 -0700
+++ b/test/langtools/ProblemList.txt	Mon Jun 18 13:40:16 2018 -0700
@@ -26,7 +26,6 @@
 ###########################################################################
 #
 # javadoc
-jdk/javadoc/tool/VerifyLocale.java                                              8149565    generic-all    -locale option issues
 jdk/javadoc/tool/enum/docComments/Main.java                                     8152313    generic-all    convert to doclet test framework
 jdk/javadoc/tool/enum/enumType/Main.java                                        8152313    generic-all    convert to doclet test framework
 jdk/javadoc/tool/varArgs/Main.java                                              8152313    generic-all    convert to doclet test framework
--- a/test/langtools/jdk/javadoc/tool/VerifyLocale.java	Mon Jun 18 10:13:58 2018 -0700
+++ b/test/langtools/jdk/javadoc/tool/VerifyLocale.java	Mon Jun 18 13:40:16 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 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,18 +23,24 @@
 
 /*
  * @test
- * @bug 8035473
+ * @bug 8035473 8149565
  * @summary Verify that init method works correctly.
  * @modules jdk.javadoc/jdk.javadoc.internal.tool
- * @ignore 8149565
  */
 
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.Collections;
+import java.util.List;
 import java.util.Locale;
 import java.util.Set;
 
 import javax.lang.model.SourceVersion;
 import javax.tools.Diagnostic.Kind;
+import javax.tools.DocumentationTool;
+import javax.tools.DocumentationTool.DocumentationTask;
+import javax.tools.JavaFileObject;
+import javax.tools.ToolProvider;
 
 import jdk.javadoc.doclet.Doclet;
 import jdk.javadoc.doclet.Reporter;
@@ -49,30 +55,54 @@
     Reporter reporter;
 
     public static void main(String[] args) {
-        String thisFile = "" +
-            new java.io.File(System.getProperty("test.src", "."),
-                             "VerifyLocale.java");
+        DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
+        Path thisFile =
+            Paths.get(System.getProperty("test.src", ".")).resolve("VerifyLocale.java");
+        JavaFileObject fo = tool.getStandardFileManager(null, null, null)
+                .getJavaFileObjects(thisFile).iterator().next();
+
+        int skipCount = 0;
+        int testCount = 0;
 
         for (Locale loc : Locale.getAvailableLocales()) {
+
             language = loc.getLanguage();
             country = loc.getCountry();
             variant = loc.getVariant();
+            System.err.printf("test locale: %s [%s,%s,%s] %s%n",
+                loc, language, country, variant, loc.toLanguageTag());
+
+            // skip locales for which the round-trip fails
+            if (!loc.equals(Locale.forLanguageTag(loc.toLanguageTag()))) {
+                System.err.println("skipped " + loc + "!");
+                System.err.println();
+                skipCount++;
+                continue;
+            }
+
             if (!language.equals("")) {
-                String[] command_line = {
-                    // jumble the options in some weird order
-                    "-doclet", "VerifyLocale",
-                    "-locale", language + (country.equals("") ? "" : ("_" + country + (variant.equals("") ? "" : "_" + variant))),
-                    "-docletpath", System.getProperty("test.classes", "."),
-                    thisFile
-                };
-                if (jdk.javadoc.internal.tool.Main.execute(command_line) != 0)
+                List<String> options = List.of("-locale", loc.toLanguageTag());
+                System.err.println("test options: " + options);
+                DocumentationTask t = tool.getTask(null, null, null,
+                        VerifyLocale.class, options, List.of(fo));
+                if (!t.call())
                     throw new Error("Javadoc encountered warnings or errors.");
+                testCount++;
             }
+            System.err.println();
         }
+        System.err.println("Skipped " + skipCount + " locales");
+        System.err.println("Tested " + testCount + " locales");
     }
 
     public boolean run(DocletEnvironment root) {
-        reporter.print(Kind.NOTE, "just a test: Locale is: " + locale.getDisplayName());
+        reporter.print(Kind.NOTE, String.format("doclet locale is: %s [%s,%s,%s] %s (%s)",
+                locale,
+                locale.getLanguage(),
+                locale.getCountry(),
+                locale.getVariant(),
+                locale.toLanguageTag(),
+                locale.getDisplayName()));
         return language.equals(locale.getLanguage())
                && country.equals(locale.getCountry())
                && variant.equals(locale.getVariant());