Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PDF parser: add required attributes from the document context to section #3554

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ private void addAccountStatementTransaction()
})

.section("date", "note", "sign", "amount", "note1") //
.documentContext("currency", "year")
.match("^[\\d]{2}\\.[\\d]{2}\\. (?<date>[\\d]{2}\\.[\\d]{2}\\.) " //
+ "(SEPA )?" //
+ "(?<note>(Dauerauftrag" //
Expand All @@ -399,7 +400,6 @@ private void addAccountStatementTransaction()
+ "(?<sign>(\\-|\\+)) (?<amount>[\\.,\\d]+)$")
.match("^(?<note1>.*)$") //
.assign((t, v) -> {
Map<String, String> context = type.getCurrentContext();
// Is sign --> "-" change from DEPOSIT to REMOVAL
if ("-".equals(v.get("sign")))
t.setType(AccountTransaction.Type.REMOVAL);
Expand All @@ -414,8 +414,8 @@ private void addAccountStatementTransaction()
if (v.get("note").startsWith("Verwendungszweck"))
v.put("note", "");

t.setDateTime(asDate(v.get("date") + context.get("year")));
t.setCurrencyCode(context.get("currency"));
t.setDateTime(asDate(v.get("date") + v.get("year")));
t.setCurrencyCode(v.get("currency"));
t.setAmount(asAmount(v.get("amount")));
t.setNote(v.get("note"));

Expand Down Expand Up @@ -447,7 +447,7 @@ private void addAccountStatementTransaction()
if (t.getCurrencyCode() != null && t.getAmount() == 0)
item.setFailureMessage(Messages.MsgErrorTransactionTypeNotSupported);

if (Boolean.valueOf(type.getCurrentContext().getBoolean("skipTransaction")))
if (type.getCurrentContext().getBoolean("skipTransaction"))
return null;

// If we have multiple entries in the document,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void parse(String filename, List<Item> items, String text)
parseContext(context, lines);

for (Block block : blocks)
block.parse(filename, items, lines);
block.parse(filename, context, items, lines);
}

/**
Expand Down Expand Up @@ -160,7 +160,7 @@ public void set(Transaction<?> transaction)
this.transaction = transaction;
}

public void parse(String filename, List<Item> items, String[] lines)
public void parse(String filename, DocumentContext documentContext, List<Item> items, String[] lines)
{
List<Integer> blocks = new ArrayList<>();

Expand Down Expand Up @@ -193,7 +193,7 @@ public void parse(String filename, List<Item> items, String[] lines)
endLine = Math.min(endLine, startLine + maxSize - 1);
}

transaction.parse(filename, items, lines, startLine, endLine);
transaction.parse(filename, documentContext, items, lines, startLine, endLine);
}
}

Expand Down Expand Up @@ -268,15 +268,16 @@ private final Transaction<T> internalOneOf(boolean isOptional,
sections.add(new Section<T>(this, null)
{
@Override
public void parse(String filename, String[] lines, int lineNo, int lineNoEnd, TypedMap ctx, T target)
public void parse(String filename, DocumentContext documentContext, String[] lines, int lineNo,
int lineNoEnd, TypedMap ctx, T target)
{
List<String> errors = new ArrayList<>();

for (Section<T> section : subSections)
{
try
{
section.parse(filename, lines, lineNo, lineNoEnd, ctx, target);
section.parse(filename, documentContext, lines, lineNo, lineNoEnd, ctx, target);

// if parsing was successful, then return
return;
Expand Down Expand Up @@ -315,14 +316,15 @@ public Transaction<T> wrap(BiFunction<T, TypedMap, Item> wrapper)
return this;
}

public void parse(String filename, List<Item> items, String[] lines, int lineNoStart, int lineNoEnd)
public void parse(String filename, DocumentContext documentContext, List<Item> items, String[] lines,
int lineNoStart, int lineNoEnd)
{
TypedMap txContext = new TypedMap();

T target = supplier.get();

for (Section<T> section : sections)
section.parse(filename, lines, lineNoStart, lineNoEnd, txContext, target);
section.parse(filename, documentContext, lines, lineNoStart, lineNoEnd, txContext, target);

for (Consumer<T> conclude : concludes)
conclude.accept(target);
Expand Down Expand Up @@ -457,7 +459,11 @@ public Set<Entry<String, String>> entrySet()
private boolean isOptional = false;
private boolean isMultipleTimes = false;
private Transaction<T> transaction;
/** attributes extracted from regular pattern */
private String[] attributes;
/** attributes mixed in from the document context */
private String[] documentAttributes;

private List<Pattern> pattern = new ArrayList<>();
private BiConsumer<T, ParsedData> assignment;

Expand All @@ -473,6 +479,12 @@ public Section<T> attributes(String... attributes)
return this;
}

public Section<T> documentContext(String... documentAttributes)
{
this.documentAttributes = documentAttributes;
return this;
}

public Section<T> optional()
{
this.isOptional = true;
Expand Down Expand Up @@ -503,7 +515,8 @@ public Transaction<T> assign(BiConsumer<T, ParsedData> assignment)
return transaction;
}

public void parse(String filename, String[] lines, int lineNo, int lineNoEnd, TypedMap txContext, T target)
public void parse(String filename, DocumentContext documentContext, String[] lines, int lineNo, int lineNoEnd,
TypedMap txContext, T target)
{
if (assignment == null)
throw new IllegalArgumentException("Assignment function missing"); //$NON-NLS-1$
Expand Down Expand Up @@ -533,6 +546,22 @@ public void parse(String filename, String[] lines, int lineNo, int lineNoEnd, Ty
Messages.MsgErrorMissingValueMatches, values.keySet().toString(),
Arrays.toString(attributes), filename, lineNo + 1, lineNoEnd + 1));

// enrich extracted values with context values
if (documentAttributes != null)
{
for (String attribute : documentAttributes)
{
if (!documentContext.containsKey(attribute))
{
throw new IllegalArgumentException(MessageFormat.format(
Messages.MsgErrorMissingValueMatches, values.keySet().toString(),
attribute, filename, lineNo + 1, lineNoEnd + 1));
}

values.put(attribute, documentContext.get(attribute));
}
}

assignment.accept(target, new ParsedData(values, lineNo, lineNoEnd, filename, txContext));

// if there might be multiple occurrences that match,
Expand Down