This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit c8e8207f1fb00dc6fafa1415a13c9f1118d9e9b4
Author: Martin Desruisseaux <martin.desruisse...@geomatys.com>
AuthorDate: Mon Dec 4 14:56:25 2023 +0100

    Avoid broken rendering of non-ASCII characters when downloading EPSG data 
from JShell.
---
 .../apache/sis/console/ResourcesDownloader.java    | 42 ++++++++++++++++++----
 1 file changed, 36 insertions(+), 6 deletions(-)

diff --git 
a/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/ResourcesDownloader.java
 
b/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/ResourcesDownloader.java
index 9ccedb690f..f33658b8f1 100644
--- 
a/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/ResourcesDownloader.java
+++ 
b/endorsed/src/org.apache.sis.console/main/org/apache/sis/console/ResourcesDownloader.java
@@ -22,6 +22,7 @@ import java.util.HashMap;
 import java.util.Locale;
 import java.util.ResourceBundle;
 import java.io.Console;
+import java.io.PrintWriter;
 import org.apache.sis.util.internal.X364;
 import org.apache.sis.system.Fallback;
 import org.apache.sis.setup.OptionalInstallations;
@@ -37,7 +38,7 @@ import org.apache.sis.setup.OptionalInstallations;
  * </ul>
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 1.1
+ * @version 1.5
  * @since   0.7
  */
 @Fallback
@@ -47,6 +48,12 @@ public class ResourcesDownloader extends 
OptionalInstallations {
      */
     private final Console console;
 
+    /**
+     * Where to write. It should be {@code console.writer()},
+     * but the latter seems partially broken when used from JShell.
+     */
+    private final PrintWriter out;
+
     /**
      * The locale to use for text display.
      */
@@ -66,6 +73,7 @@ public class ResourcesDownloader extends 
OptionalInstallations {
     /**
      * Creates a new installation scripts provider.
      */
+    @SuppressWarnings("UseOfSystemOutOrSystemErr")
     public ResourcesDownloader() {
         super("text/plain");
         final CommandRunner command = CommandRunner.instance.get();
@@ -77,6 +85,7 @@ public class ResourcesDownloader extends 
OptionalInstallations {
             colors = false;
         }
         console = System.console();
+        out = CommandRunner.writer(console, System.out);
     }
 
     /**
@@ -107,6 +116,10 @@ public class ResourcesDownloader extends 
OptionalInstallations {
      * This method may be invoked twice for the same {@code authority} 
argument:
      * first with a null {@code license} argument for asking if the user 
agrees to download the data,
      * then with a non-null {@code license} argument for asking if the user 
agrees with the license terms.
+     *
+     * @param  authority  "EPSG".
+     * @param  license    the license, or {@code null} for asking if the user 
wants to download the data.
+     * @return whether user accepted.
      */
     @Override
     protected boolean askUserAgreement(final String authority, final String 
license) {
@@ -131,18 +144,21 @@ public class ResourcesDownloader extends 
OptionalInstallations {
         /*
          * Show the question.
          */
+        final String lineSeparator = System.lineSeparator();
         final String prompt, action;
         if (license == null) {
             prompt = "download";
             action = "downloading";
-            console.format(resources.getString("install"), textColor, 
getSpaceRequirement(authority),
+            format(resources.getString("install"), textColor, 
getSpaceRequirement(authority),
                            linkColor, destinationDirectory, linkOff, 
resetColor);
         } else {
             prompt = "accept";
             action = "installing";
-            console.format("%n").writer().write(license);
-            console.format("%n");
+            out.write(lineSeparator);
+            out.write(license);
+            out.write(lineSeparator);
         }
+        out.flush();
         /*
          * Ask user agreement.
          */
@@ -151,10 +167,24 @@ public class ResourcesDownloader extends 
OptionalInstallations {
             answer = answers.get(console.readLine(resources.getString(prompt), 
textColor, resetColor).toLowerCase(getLocale()));
         } while (answer == null);
         if (answer) {
-            console.format(resources.getString(action), actionColor, 
resetColor);
+            format(resources.getString(action), actionColor, resetColor);
         } else {
-            console.format("%n");
+            out.write(lineSeparator);
         }
         return answer;
     }
+
+    /**
+     * Writes a formatted output to the console.
+     *
+     * @param pattern  pattern to write.
+     * @param args  arguments referenced by the pattern.
+     */
+    private void format(final String pattern, final Object... args) {
+        if (console.writer() == out) {
+            console.format(pattern, args);
+        } else {
+            out.format(pattern, args);
+        }
+    }
 }

Reply via email to