package com.box.l10n.mojito.okapi.filters;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 *
 * @author emagalindan
 */
@Component
public class UnescapeUtils {

    /**
     * Logger
     */
    static Logger logger = LoggerFactory.getLogger(UnescapeUtils.class);

    /**
     * Unescapes line feed, cariage return, single quote and double quote
     *
     * @param text
     * @return
     */
    public String unescape(String text) {
        String unescapedText = replaceEscapedCarriageReturn(text);
        unescapedText = replaceEscapedLineFeed(unescapedText);
        unescapedText = replaceEscapedQuotes(unescapedText);
        return unescapedText;
    }

    String replaceEscapedCarriageReturn(String text) {
        return text.replaceAll("\\\\r", "\r");
    }

    String replaceEscapedLineFeed(String text) {
        return text.replaceAll("\\\\n", "\n");
    }

    /**
     * Replaces \' and \" with ' or "
     *
     * @param text
     * @return
     */
    String replaceEscapedQuotes(String text) {
        return text.replaceAll("\\\\(\"|')", "$1");
    }

    /**
     * Replace other escape character with the character itself.
     *
     * Must be call after replacing espace sequence that need a different treatment like {@link #replaceEscapedLineFeed(String)}
     *
     * @param text
     * @return
     */
    String replaceEscapedCharacters(String text) {
        return text.replaceAll("\\\\(.)?", "$1");
    }

    /**
     * Collapse multiple spaces into a single space.
     *
     * @param text
     * @return
     */
    String collapseSpaces(String text) {
        return text.replaceAll("\\s+", " ");
    }

    String replaceLineFeedWithSpace(String text) {
        return text.replaceAll("\n", " ");
    }
}

@@ -148,15 +148,45 @@
 t;%0A%0A
+import java.util.regex.Pattern;%0A%0A
 /**%0A
- *%0A
  * @
@@ -353,16 +353,479 @@
 lass);%0A%0A
+    private static final Pattern ESCAPED_CARIAGE_RETURN = Pattern.compile(%22%5C%5C%5C%5Cr%22);%0A    private static final Pattern ESCAPED_LINE_FEED = Pattern.compile(%22%5C%5C%5C%5Cn%22);%0A    private static final Pattern ESCAPED_QUOTES = Pattern.compile(%22%5C%5C%5C%5C(%5C%22%7C')%22);%0A    private static final Pattern ESCAPED_CHARACTERS = Pattern.compile(%22%5C%5C%5C%5C(.)?%22);%0A    private static final Pattern SPACES = Pattern.compile(%22%5C%5Cs+%22);%0A    private static final Pattern LINE_FEED = Pattern.compile(%22%5Cn%22);%0A%0A
     /**%0A
@@ -1279,36 +1279,68 @@
 %0A        return 
+ESCAPED_CARIAGE_RETURN.matcher(
 text
+)
 .replaceAll(%22%5C%5C%5C
@@ -1339,17 +1339,8 @@
 All(
-%22%5C%5C%5C%5Cr%22, 
 %22%5Cr%22
@@ -1405,36 +1405,63 @@
 %0A        return 
+ESCAPED_LINE_FEED.matcher(
 text
+)
 .replaceAll(%22%5C%5C%5C
@@ -1460,17 +1460,8 @@
 All(
-%22%5C%5C%5C%5Cn%22, 
 %22%5Cn%22
@@ -1615,36 +1615,59 @@
 t) %7B%0A       
- 
 return 
+ESCAPED_QUOTES.matcher(
 text
+)
 .replaceAll(
@@ -1670,22 +1670,8 @@
 All(
-%22%5C%5C%5C%5C(%5C%22%7C')%22, 
 %22$1%22
@@ -1751,32 +1751,36 @@
 r itself.%0A     *
+ %3Cp%3E
 %0A     * Must be 
@@ -2000,36 +2000,64 @@
 %0A        return 
+ESCAPED_CHARACTERS.matcher(
 text
+)
 .replaceAll(%22%5C%5C%5C
@@ -2056,20 +2056,8 @@
 All(
-%22%5C%5C%5C%5C(.)?%22, 
 %22$1%22
@@ -2224,36 +2224,52 @@
 %0A        return 
+SPACES.matcher(
 text
+)
 .replaceAll(%22%5C%5Cs
@@ -2269,16 +2269,8 @@
 ll(%22
-%5C%5Cs+%22, %22
  %22);
@@ -2343,20 +2343,39 @@
  return 
+LINE_FEED.matcher(
 text
+)
 .replace
@@ -2383,14 +2383,8 @@
 ll(%22
-%5Cn%22, %22
  %22);

Use compile patterns