- //L(String.format("State is %d", updating));
- switch (state) {
- case EXPECTING_ACCOUNT:
- if (line.equals("<h2>General Journal</h2>")) {
- state = ParserState.EXPECTING_TRANSACTION;
- L("→ expecting transaction");
- // commit the current transaction and start a new one
- // the account list in the UI should reflect the (committed)
- // state of the database
- db.setTransactionSuccessful();
- db.endTransaction();
- Data.accounts.setList(accountList);
- db.beginTransaction();
- continue;
- }
- m = reAccountName.matcher(line);
- if (m.find()) {
- String acct_encoded = m.group(1);
- String acct_name = URLDecoder.decode(acct_encoded, "UTF-8");
- acct_name = acct_name.replace("\"", "");
- L(String.format("found account: %s", acct_name));
-
- prevAccount = lastAccount;
- lastAccount = profile.tryLoadAccount(db, acct_name);
- if (lastAccount == null)
- lastAccount = new LedgerAccount(acct_name);
- else lastAccount.removeAmounts();
- profile.storeAccount(db, lastAccount);
-
- if (prevAccount != null) prevAccount
- .setHasSubAccounts(prevAccount.isParentOf(lastAccount));
- // make sure the parent account(s) are present,
- // synthesising them if necessary
- // this happens when the (missing-in-HTML) parent account has
- // only one child so we create a synthetic parent account record,
- // copying the amounts when child's amounts are parsed
- String parentName = lastAccount.getParentName();
- if (parentName != null) {
- Stack<String> toAppend = new Stack<>();
- while (parentName != null) {
- if (accountNames.containsKey(parentName)) break;
- toAppend.push(parentName);
- parentName =
- new LedgerAccount(parentName).getParentName();
- }
- syntheticAccounts.clear();
- while (!toAppend.isEmpty()) {
- String aName = toAppend.pop();
- LedgerAccount acc = profile.tryLoadAccount(db, aName);
- if (acc == null) {
- acc = new LedgerAccount(aName);
- acc.setHiddenByStar(lastAccount.isHiddenByStar());
- acc.setExpanded(!lastAccount.hasSubAccounts() ||
- lastAccount.isExpanded());
- }
- acc.setHasSubAccounts(true);
- acc.removeAmounts(); // filled below when amounts are parsed
- if ((!onlyStarred || !acc.isHiddenByStar()) &&
- acc.isVisible(accountList)) accountList.add(acc);
- L(String.format("gap-filling with %s", aName));
- accountNames.put(aName, null);
- profile.storeAccount(db, acc);
- syntheticAccounts.put(aName, acc);
- }
- }
-
- if ((!onlyStarred || !lastAccount.isHiddenByStar()) &&
- lastAccount.isVisible(accountList))
- accountList.add(lastAccount);
- accountNames.put(acct_name, null);
-
- state = ParserState.EXPECTING_ACCOUNT_AMOUNT;
- L("→ expecting account amount");
- }
- break;
-
- case EXPECTING_ACCOUNT_AMOUNT:
- m = reAccountValue.matcher(line);
- boolean match_found = false;
- while (m.find()) {
- throwIfCancelled();
-
- match_found = true;
- String value = m.group(1);
- String currency = m.group(2);
- if (currency == null) currency = "";
+ m = reAccountName.matcher(line);
+ if (m.find()) {
+ String acct_encoded = m.group(1);
+ String accName = URLDecoder.decode(acct_encoded, "UTF-8");
+ accName = accName.replace("\"", "");
+ L(String.format("found account: %s", accName));
+
+ lastAccount = map.get(accName);
+ if (lastAccount != null) {
+ L(String.format("ignoring duplicate account '%s'", accName));
+ continue;
+ }
+ String parentAccountName = LedgerAccount.extractParentName(accName);
+ LedgerAccount parentAccount;
+ if (parentAccountName != null) {
+ parentAccount = ensureAccountExists(parentAccountName, map,
+ syntheticAccounts);
+ }
+ else {
+ parentAccount = null;
+ }
+ lastAccount = new LedgerAccount(profile, accName, parentAccount);
+
+ accounts.add(lastAccount);
+ map.put(accName, lastAccount);
+
+ state = ParserState.EXPECTING_ACCOUNT_AMOUNT;
+ L("→ expecting account amount");
+ }
+ break;
+
+ case EXPECTING_ACCOUNT_AMOUNT:
+ m = reAccountValue.matcher(line);
+ boolean match_found = false;
+ while (m.find()) {
+ throwIfCancelled();
+
+ match_found = true;
+ String value = Objects.requireNonNull(m.group(1));
+ String currency = m.group(2);
+ if (currency == null)
+ currency = "";
+
+ {
+ Matcher tmpM = reDecimalComma.matcher(value);
+ if (tmpM.find()) {
+ value = value.replace(".", "");