package org.javarosa.core.services.locale;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Hashtable;

/**
 * @author ctsims
 */
public class LocalizationUtils {
    /**
     * @param is A path to a resource file provided in the current environment
     * @return a dictionary of key/value locale pairs from a file in the resource directory
     */
    public static Hashtable<String, String> parseLocaleInput(InputStream is) throws IOException {
        Hashtable<String, String> locale = new Hashtable<>();
        InputStreamReader isr = new InputStreamReader(is, "UTF-8");
        BufferedReader reader = new BufferedReader(isr);

        String line = reader.readLine();
        int lineCount = 0;
        while (line != null) {
            parseAndAdd(locale, line, lineCount++);
            line = reader.readLine();
        }

        return locale;
    }

    public static void parseAndAdd(Hashtable<String, String> locale, String line, int curline) {
        line = line.trim();

        int i = 0;
        int dec = line.length();

        //clear comments except if they have backslash before them (markdown '#'s)
        while ((i = line.substring(0, dec).lastIndexOf("#")) != -1) {
            if ((i == 0) || !(line.charAt(i - 1) == '\\')) {
                line = line.substring(0, i);
                dec = line.length();
            } else {
                dec = i;
            }
        }

        int equalIndex = line.indexOf('=');
        if (equalIndex == -1) {
            if (!line.trim().equals("")) {
                System.out.println("Invalid line (#" + curline + ") read: " + line);
            }
        } else {
            //Check to see if there's anything after the '=' first. Otherwise there
            //might be some big problems.
            if (equalIndex != line.length() - 1) {
                String value = line.substring(equalIndex + 1, line.length());
                locale.put(line.substring(0, equalIndex), parseValue(value));
            }
        }
    }

    /**
     * Replace markdown encodings
     */
    public static String parseValue(String value) {
        return value.replaceAll("\\\\#", "#").replaceAll("\\\\n", "\n");
    }
}

@@ -1231,16 +1231,46 @@
 e ((i = 
+LocalizationUtils.lastIndexOf(
 line.sub
@@ -1283,29 +1283,18 @@
 (0, dec)
-.lastIndexOf(
+, 
 %22#%22)) !=
@@ -2227,53 +2227,106 @@
     
-return value.replaceAll(%22%5C%5C%5C%5C#%22, %22#%22)
+String ret = LocalizationUtils.replace(value, %22%5C%5C#%22, %22#%22);%0A        ret = LocalizationUtils
 .replace
 All(
@@ -2325,15 +2325,15 @@
 lace
-All(%22%5C%5C
+(ret, %22
 %5C%5Cn%22
@@ -2337,20 +2337,1017 @@
 %5Cn%22, %22%5Cn%22);%0A
+        return ret;%0A    %7D%0A%0A    /**%0A     * http://stackoverflow.com/questions/10626606/replace-string-with-string-in-j2me%0A     */%0A    private static String replace(String str, String pattern, String replace) %7B%0A        int s = 0;%0A        int e = 0;%0A        StringBuffer result = new StringBuffer();%0A%0A        while ((e = str.indexOf(pattern, s)) %3E= 0) %7B%0A            result.append(str.substring(s, e));%0A            result.append(replace);%0A            s = e + pattern.length();%0A        %7D%0A        result.append(str.substring(s));%0A        return result.toString();%0A    %7D%0A%0A    /**%0A     * http://www.experts-exchange.com/Programming/Languages/Java/Q_27604323.html%0A     */%0A    private static int lastIndexOf(String str, String search) %7B%0A        int i = 0;%0A        int offset = 0;%0A        int found = -1;%0A%0A        while (offset %3C str.length()) %7B%0A            i = str.indexOf(search, offset);%0A            if (i == -1) break;%0A%0A            found = i;%0A%0A            offset = i + 1;%0A        %7D%0A        return found;%0A
     %7D%0A%7D%0A

Revert "Remove custom J2ME replace/index functions"