karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbono...@apache.org
Subject git commit: [KARAF-3327] Improve LC_CTYPE parsing
Date Mon, 03 Nov 2014 20:26:01 GMT
Repository: karaf
Updated Branches:
  refs/heads/karaf-3.0.x 3a67dbcdf -> d46eea6ed


[KARAF-3327] Improve LC_CTYPE parsing


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/d46eea6e
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/d46eea6e
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/d46eea6e

Branch: refs/heads/karaf-3.0.x
Commit: d46eea6edc214957815185943fea122a53f6d199
Parents: 3a67dbc
Author: Jean-Baptiste Onofré <jbonofre@apache.org>
Authored: Mon Nov 3 21:25:15 2014 +0100
Committer: Jean-Baptiste Onofré <jbonofre@apache.org>
Committed: Mon Nov 3 21:25:15 2014 +0100

----------------------------------------------------------------------
 .../console/impl/jline/LocalConsoleManager.java | 42 ++++++++++++++++----
 .../karaf/shell/ssh/ShellFactoryImpl.java       | 40 +++++++++++++++++--
 2 files changed, 71 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/d46eea6e/shell/console/src/main/java/org/apache/karaf/shell/console/impl/jline/LocalConsoleManager.java
----------------------------------------------------------------------
diff --git a/shell/console/src/main/java/org/apache/karaf/shell/console/impl/jline/LocalConsoleManager.java
b/shell/console/src/main/java/org/apache/karaf/shell/console/impl/jline/LocalConsoleManager.java
index e9e6f70..787c2da 100644
--- a/shell/console/src/main/java/org/apache/karaf/shell/console/impl/jline/LocalConsoleManager.java
+++ b/shell/console/src/main/java/org/apache/karaf/shell/console/impl/jline/LocalConsoleManager.java
@@ -92,13 +92,7 @@ public class LocalConsoleManager {
                 }
             }
         };
-        String ctype = System.getenv("LC_CTYPE");
-        final String encoding;
-        if (ctype != null && ctype.indexOf('.') > 0) {
-            encoding = ctype.substring(ctype.indexOf('.') + 1);
-        } else {
-            encoding = System.getProperty("input.encoding", Charset.defaultCharset().name());
-        }
+        final String encoding = getEncoding();
 
         Runnable consoleStarter = new Runnable() {
             public void run() {
@@ -142,4 +136,38 @@ public class LocalConsoleManager {
         }
     }
 
+    /**
+     * Get the default encoding.  Will first look at the LC_CTYPE environment variable, then
the input.encoding
+     * system property, then the default charset according to the JVM.
+     *
+     * @return The default encoding to use when none is specified.
+     */
+    public static String getEncoding() {
+        // LC_CTYPE is usually in the form en_US.UTF-8
+        String envEncoding = extractEncodingFromCtype(System.getenv("LC_CTYPE"));
+        if (envEncoding != null) {
+            return envEncoding;
+        }
+        return System.getProperty("input.encoding", Charset.defaultCharset().name());
+    }
+
+    /**
+     * Parses the LC_CTYPE value to extract the encoding according to the POSIX standard,
which says that the LC_CTYPE
+     * environment variable may be of the format <code>[language[_territory][.codeset][@modifier]]</code>
+     *
+     * @param ctype The ctype to parse, may be null
+     * @return The encoding, if one was present, otherwise null
+     */
+    static String extractEncodingFromCtype(String ctype) {
+        if (ctype != null && ctype.indexOf('.') > 0) {
+            String encodingAndModifier = ctype.substring(ctype.indexOf('.') + 1);
+            if (encodingAndModifier.indexOf('@') > 0) {
+                return encodingAndModifier.substring(0, encodingAndModifier.indexOf('@'));
+            } else {
+                return encodingAndModifier;
+            }
+        }
+        return null;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/karaf/blob/d46eea6e/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/ShellFactoryImpl.java
----------------------------------------------------------------------
diff --git a/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/ShellFactoryImpl.java b/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/ShellFactoryImpl.java
index c9a3dff..f919ebb 100644
--- a/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/ShellFactoryImpl.java
+++ b/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/ShellFactoryImpl.java
@@ -24,6 +24,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.PrintStream;
+import java.nio.charset.Charset;
 import java.security.PrivilegedAction;
 import java.util.Map;
 
@@ -129,10 +130,7 @@ public class ShellFactoryImpl implements Factory<Command> {
                                 ShellImpl.this.destroy();
                             }
                         };
-                        String encoding = env.getEnv().get("LC_CTYPE");
-                        if (encoding != null && encoding.indexOf('.') > 0) {
-                            encoding = encoding.substring(encoding.indexOf('.') + 1);
-                        }
+                        String encoding = getEncoding();
                         final Console console = consoleFactory.create(commandProcessor, threadIO,
in,
                                 lfToCrLfPrintStream(out), lfToCrLfPrintStream(err), terminal,
encoding, destroyCallback);
                         final CommandSession session = console.getSession();
@@ -172,6 +170,40 @@ public class ShellFactoryImpl implements Factory<Command> {
 
     }
 
+    /**
+     * Get the default encoding.  Will first look at the LC_CTYPE environment variable, then
the input.encoding
+     * system property, then the default charset according to the JVM.
+     *
+     * @return The default encoding to use when none is specified.
+     */
+    public static String getEncoding() {
+        // LC_CTYPE is usually in the form en_US.UTF-8
+        String envEncoding = extractEncodingFromCtype(System.getenv("LC_CTYPE"));
+        if (envEncoding != null) {
+            return envEncoding;
+        }
+        return System.getProperty("input.encoding", Charset.defaultCharset().name());
+    }
+
+    /**
+     * Parses the LC_CTYPE value to extract the encoding according to the POSIX standard,
which says that the LC_CTYPE
+     * environment variable may be of the format <code>[language[_territory][.codeset][@modifier]]</code>
+     *
+     * @param ctype The ctype to parse, may be null
+     * @return The encoding, if one was present, otherwise null
+     */
+    static String extractEncodingFromCtype(String ctype) {
+        if (ctype != null && ctype.indexOf('.') > 0) {
+            String encodingAndModifier = ctype.substring(ctype.indexOf('.') + 1);
+            if (encodingAndModifier.indexOf('@') > 0) {
+                return encodingAndModifier.substring(0, encodingAndModifier.indexOf('@'));
+            } else {
+                return encodingAndModifier;
+            }
+        }
+        return null;
+    }
+
     private static void flush(OutputStream... streams) {
         for (OutputStream s : streams) {
             try {


Mime
View raw message