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

markt pushed a commit to branch 8.5.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit 5ca6279c7c23e2528ec0fc95894305fdf938760e
Author: Mark Thomas <[email protected]>
AuthorDate: Tue May 25 22:02:42 2021 +0100

    Update import/export to handle POEditor behaviours
    
    The end results for
    Tomcat-Export->Tomcat-Import
    and
    Tomcat-Export->POEditor-Import->POEditor-Export->Tomcat-Import
    are now much more similar
---
 .../apache/tomcat/buildutil/translate/Import.java  |  2 +-
 .../apache/tomcat/buildutil/translate/Utils.java   | 53 ++++++++++++++++------
 2 files changed, 41 insertions(+), 14 deletions(-)

diff --git a/java/org/apache/tomcat/buildutil/translate/Import.java 
b/java/org/apache/tomcat/buildutil/translate/Import.java
index 3733248..1dcdd12 100644
--- a/java/org/apache/tomcat/buildutil/translate/Import.java
+++ b/java/org/apache/tomcat/buildutil/translate/Import.java
@@ -78,7 +78,7 @@ public class Import {
                 w.write(System.lineSeparator());
             }
 
-            w.write(cKey.key + "=" + Utils.formatValue(value));
+            w.write(cKey.key + "=" + Utils.formatValueImport(value));
             w.write(System.lineSeparator());
         }
         if (w != null) {
diff --git a/java/org/apache/tomcat/buildutil/translate/Utils.java 
b/java/org/apache/tomcat/buildutil/translate/Utils.java
index 63d3947..80a7756 100644
--- a/java/org/apache/tomcat/buildutil/translate/Utils.java
+++ b/java/org/apache/tomcat/buildutil/translate/Utils.java
@@ -32,9 +32,10 @@ import java.util.regex.Pattern;
 
 public class Utils {
 
-    private static final Pattern ADD_CONTINUATION = Pattern.compile("\\n", 
Pattern.MULTILINE);
     private static final Pattern ESCAPE_LEADING_SPACE = 
Pattern.compile("^(\\s)", Pattern.MULTILINE);
-    private static final Pattern FIX_SINGLE_QUOTE = 
Pattern.compile("(?<!')'(?!')", Pattern.MULTILINE);
+
+    // Package private so it is visible to tests
+    static final String PADDING = "POEDITOR_EXPORT_PADDING_DO_NOT_DELETE";
 
     private Utils() {
         // Utility class. Hide default constructor.
@@ -59,21 +60,47 @@ public class Utils {
     }
 
 
-    static String formatValue(String in) {
-        String result = ADD_CONTINUATION.matcher(in).replaceAll("\\\\n\\\\\n");
-        if (result.endsWith("\\\n")) {
-            result = result.substring(0, result.length() - 2);
+    static String formatValueExport(String in) {
+        String result;
+
+        if (in.startsWith("\n")) {
+            result = PADDING + in;
+        } else {
+            result = in;
         }
-        result = ESCAPE_LEADING_SPACE.matcher(result).replaceAll("\\\\$1");
 
-        if (result.contains("\n\\\t")) {
-            result = result.replace("\n\\\t", "\n\\t");
+        return formatValueCommon(result);
+    }
+
+
+    static String formatValueImport(String in) {
+        String result;
+
+        if (in.startsWith(PADDING)) {
+            result = in.substring(PADDING.length());
+        } else {
+            result = in;
         }
 
-        if (result.contains("[{0}]")) {
-            result = FIX_SINGLE_QUOTE.matcher(result).replaceAll("''");
+        return formatValueCommon(result);
+    }
+
+
+    /*
+     * Common formatting to convert a String for storage as a value in a
+     * property file.
+     */
+    static String formatValueCommon(String in) {
+        String result = in.replace("\n", "\\n\\\n");
+        if (result.endsWith("\\n\\\n")) {
+            result = result.substring(0, result.length() - 2);
         }
-        return result.trim();
+
+        result = ESCAPE_LEADING_SPACE.matcher(result).replaceAll("\\\\$1");
+
+        result = result.replaceAll("\t", "\\t");
+
+        return result;
     }
 
 
@@ -140,7 +167,7 @@ public class Utils {
             String[] keys = translation.keySet().toArray(new String[0]);
             Arrays.sort(keys);
             for (Object key : keys) {
-                w.write(key + "=" + 
Utils.formatValue(translation.getProperty((String) key)) + "\n");
+                w.write(key + "=" + 
Utils.formatValueExport(translation.getProperty((String) key)) + "\n");
             }
         } catch (IOException ioe) {
             ioe.printStackTrace();

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to