]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java
legacy parser: do not crash with amounts like '1,234.56'
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / RetrieveTransactionsTask.java
index c1406d250d1d9462931858eddaffb63a55ee0ae0..0354e5ed98e034ae5baee6bf82d17b374a4f14e3 100644 (file)
@@ -22,6 +22,8 @@ import android.database.sqlite.SQLiteDatabase;
 import android.os.AsyncTask;
 import android.os.OperationCanceledException;
 
+import androidx.annotation.NonNull;
+
 import net.ktnx.mobileledger.App;
 import net.ktnx.mobileledger.err.HTTPException;
 import net.ktnx.mobileledger.json.AccountListParser;
@@ -54,8 +56,6 @@ import java.util.Stack;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-import androidx.annotation.NonNull;
-
 import static net.ktnx.mobileledger.utils.Logger.debug;
 
 
@@ -70,6 +70,8 @@ public class RetrieveTransactionsTask
     private static final Pattern reTransactionDetails =
             Pattern.compile("^\\s+(\\S[\\S\\s]+\\S)\\s\\s+([-+]?\\d[\\d,.]*)(?:\\s+(\\S+)$)?");
     private static final Pattern reEnd = Pattern.compile("\\bid=\"addmodal\"");
+    private static final Pattern reDecimalPoint = Pattern.compile("\\.\\d\\d?$");
+    private static final Pattern reDecimalComma = Pattern.compile(",\\d\\d?$");
     private WeakReference<MainActivity> contextRef;
     // %3A is '='
     private Pattern reAccountName = Pattern.compile("/register\\?q=inacct%3A([a-zA-Z0-9%]+)\"");
@@ -245,18 +247,34 @@ public class RetrieveTransactionsTask
                                 String value = m.group(1);
                                 String currency = m.group(2);
                                 if (currency == null) currency = "";
-                                value = value.replace(',', '.');
+
+                                {
+                                    Matcher tmpM = reDecimalComma.matcher(value);
+                                    if (tmpM.find()) {
+                                        value = value.replace(".", "");
+                                        value = value.replace(',', '.');
+                                    }
+
+                                    tmpM = reDecimalPoint.matcher(value);
+                                    if (tmpM.find()) {
+                                        value = value.replace(",", "");
+                                        value = value.replace(" ", "");
+                                    }
+                                }
                                 L("curr=" + currency + ", value=" + value);
                                 final float val = Float.parseFloat(value);
                                 profile.storeAccountValue(db, lastAccount.getName(), currency, val);
                                 lastAccount.addAmount(val, currency);
                                 for (LedgerAccount syn : syntheticAccounts.values()) {
+                                    L(String.format(Locale.ENGLISH, "propagating %s %1.2f to %s",
+                                            currency, val, syn.getName()));
                                     syn.addAmount(val, currency);
                                     profile.storeAccountValue(db, syn.getName(), currency, val);
                                 }
                             }
 
                             if (match_found) {
+                                syntheticAccounts.clear();
                                 state = ParserState.EXPECTING_ACCOUNT;
                                 L("→ expecting account");
                             }
@@ -391,8 +409,7 @@ public class RetrieveTransactionsTask
                 new String[]{profile.getUuid()});
         db.execSQL("update accounts set keep=0 where profile=?;", new String[]{profile.getUuid()});
     }
-    private boolean retrieveAccountList()
-            throws IOException, HTTPException {
+    private boolean retrieveAccountList() throws IOException, HTTPException {
         Progress progress = new Progress();
 
         HttpURLConnection http = NetworkUtil.prepareConnection(profile, "accounts");
@@ -479,8 +496,7 @@ public class RetrieveTransactionsTask
 
         return true;
     }
-    private boolean retrieveTransactionList()
-            throws IOException, ParseException, HTTPException {
+    private boolean retrieveTransactionList() throws IOException, ParseException, HTTPException {
         Progress progress = new Progress();
         int maxTransactionId = Progress.INDETERMINATE;