Skip to content

Commit 131dd03

Browse files
authored
Merge pull request #253 from metafacture/251-strictnessHandlesProcessExceptions
Optionally apply "strict" mode to `FixProcessException`s.
2 parents 917782f + b63e8a7 commit 131dd03

File tree

7 files changed

+95
-14
lines changed

7 files changed

+95
-14
lines changed

metafix/src/main/java/org/metafacture/metafix/Metafix.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package org.metafacture.metafix;
2020

21+
import org.metafacture.framework.MetafactureException;
2122
import org.metafacture.framework.StandardEventNames;
2223
import org.metafacture.framework.StreamPipe;
2324
import org.metafacture.framework.StreamReceiver;
@@ -83,9 +84,10 @@ public class Metafix implements StreamPipe<StreamReceiver>, Maps {
8384
private Record currentRecord = new Record();
8485
private StreamReceiver outputStreamReceiver;
8586
private Strictness strictness = DEFAULT_STRICTNESS;
86-
private boolean repeatedFieldsToEntities;
8787
private String fixFile;
8888
private String recordIdentifier;
89+
private boolean repeatedFieldsToEntities;
90+
private boolean strictnessHandlesProcessExceptions;
8991
private int entityCount;
9092

9193
public Metafix() {
@@ -359,6 +361,14 @@ public Strictness getStrictness() {
359361
return strictness;
360362
}
361363

364+
public void setStrictnessHandlesProcessExceptions(final boolean strictnessHandlesProcessExceptions) {
365+
this.strictnessHandlesProcessExceptions = strictnessHandlesProcessExceptions;
366+
}
367+
368+
public boolean getStrictnessHandlesProcessExceptions() {
369+
return strictnessHandlesProcessExceptions;
370+
}
371+
362372
public void setRepeatedFieldsToEntities(final boolean repeatedFieldsToEntities) {
363373
this.repeatedFieldsToEntities = repeatedFieldsToEntities;
364374
}
@@ -374,7 +384,7 @@ public enum Strictness {
374384
*/
375385
PROCESS {
376386
@Override
377-
protected void handleInternal(final FixExecutionException exception, final Record record) {
387+
protected void handleInternal(final MetafactureException exception, final Record record) {
378388
throw exception;
379389
}
380390
},
@@ -384,7 +394,7 @@ protected void handleInternal(final FixExecutionException exception, final Recor
384394
*/
385395
RECORD {
386396
@Override
387-
protected void handleInternal(final FixExecutionException exception, final Record record) {
397+
protected void handleInternal(final MetafactureException exception, final Record record) {
388398
log(exception, LOG::error);
389399
record.setReject(true); // TODO: Skip remaining expressions?
390400
}
@@ -395,19 +405,19 @@ protected void handleInternal(final FixExecutionException exception, final Recor
395405
*/
396406
EXPRESSION {
397407
@Override
398-
protected void handleInternal(final FixExecutionException exception, final Record record) {
408+
protected void handleInternal(final MetafactureException exception, final Record record) {
399409
log(exception, LOG::warn);
400410
}
401411
};
402412

403-
public void handle(final FixExecutionException exception, final Record record) {
413+
public void handle(final MetafactureException exception, final Record record) {
404414
LOG.info("Current record: {}", record);
405415
handleInternal(exception, record);
406416
}
407417

408-
protected abstract void handleInternal(FixExecutionException exception, Record record);
418+
protected abstract void handleInternal(MetafactureException exception, Record record);
409419

410-
protected void log(final FixExecutionException exception, final BiConsumer<String, Throwable> logger) {
420+
protected void log(final MetafactureException exception, final BiConsumer<String, Throwable> logger) {
411421
logger.accept(exception.getMessage(), exception.getCause());
412422
}
413423

metafix/src/main/java/org/metafacture/metafix/RecordTransformer.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.metafacture.commons.StringUtil;
2020
import org.metafacture.commons.reflection.ReflectionUtil;
21+
import org.metafacture.framework.MetafactureException;
2122
import org.metafacture.metafix.api.FixContext;
2223
import org.metafacture.metafix.api.FixFunction;
2324
import org.metafacture.metafix.api.FixPredicate;
@@ -120,7 +121,7 @@ public void transform(final Record record, final Map<String, String> additionalV
120121

121122
public void transform(final Record record) {
122123
consumers.forEach(consumer -> {
123-
final FixExecutionException exception = tryRun(() -> consumer.accept(record));
124+
final MetafactureException exception = tryRun(() -> consumer.accept(record));
124125

125126
if (exception != null) {
126127
metafix.getStrictness().handle(exception, record);
@@ -216,7 +217,7 @@ private <T, R> List<R> mapList(final List<T> list, final Function<T, R> function
216217
private void processFix(final Supplier<String> messageSupplier, final Supplier<Consumer<Record>> consumerSupplier) {
217218
currentMessageSupplier = messageSupplier;
218219

219-
final FixExecutionException exception = tryRun(() -> {
220+
final MetafactureException exception = tryRun(() -> {
220221
final Consumer<Record> consumer = consumerSupplier.get();
221222

222223
consumers.add(record -> {
@@ -230,7 +231,7 @@ private void processFix(final Supplier<String> messageSupplier, final Supplier<C
230231
}
231232
}
232233

233-
private FixExecutionException tryRun(final Runnable runnable) { // checkstyle-disable-line ReturnCount
234+
private MetafactureException tryRun(final Runnable runnable) { // checkstyle-disable-line ReturnCount
234235
try {
235236
runnable.run();
236237
}
@@ -244,7 +245,14 @@ private FixExecutionException tryRun(final Runnable runnable) { // checkstyle-di
244245
return new FixExecutionException(currentMessageSupplier.get(), e);
245246
}
246247
catch (final RuntimeException e) { // checkstyle-disable-line IllegalCatch
247-
throw new FixProcessException(currentMessageSupplier.get(), e);
248+
final MetafactureException exception = new FixProcessException(currentMessageSupplier.get(), e);
249+
250+
if (metafix.getStrictnessHandlesProcessExceptions()) {
251+
return exception;
252+
}
253+
else {
254+
throw exception;
255+
}
248256
}
249257

250258
return null;

metafix/src/test/java/org/metafacture/metafix/MetafixScriptTest.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -435,13 +435,17 @@ public void shouldIncludeLocationAndTextInExecutionException() {
435435
);
436436
}
437437

438-
private void assertStrictness(final Metafix.Strictness strictness, final String fixDef, final boolean stubLogging, final Consumer<Supplier<StreamReceiver>> out) {
438+
private void assertStrictness(final Metafix.Strictness strictness, final String fixDef, final boolean stubLogging, final Consumer<Metafix> in, final Consumer<Supplier<StreamReceiver>> out) {
439439
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
440440
"add_field('before', '')",
441441
fixDef,
442442
"add_field('after', '')"
443443
),
444444
i -> {
445+
if (in != null) {
446+
in.accept(i);
447+
}
448+
445449
final Metafix.Strictness strictnessSpy = Mockito.spy(strictness);
446450
i.setStrictness(strictnessSpy);
447451

@@ -469,7 +473,7 @@ private void assertStrictness(final Metafix.Strictness strictness, final String
469473
}
470474

471475
private void assertStrictness(final Metafix.Strictness strictness, final boolean stubLogging, final Consumer<Supplier<StreamReceiver>> out) {
472-
assertStrictness(strictness, "upcase('data')", stubLogging, out);
476+
assertStrictness(strictness, "upcase('data')", stubLogging, null, out);
473477
}
474478

475479
@Test
@@ -524,11 +528,35 @@ public void shouldAbortProcessOnExecutionException() {
524528
@Test
525529
public void shouldAbortProcessOnProcessException() {
526530
MetafixTestHelpers.assertProcessException(IllegalArgumentException.class, "No enum constant org.metafacture.metafix.FixMethod.foo", () ->
527-
assertStrictness(Metafix.Strictness.EXPRESSION, "foo()", false, o -> {
531+
assertStrictness(Metafix.Strictness.EXPRESSION, "foo()", false, null, o -> {
528532
})
529533
);
530534
}
531535

536+
@Test
537+
public void shouldOptionallySkipExpressionOnProcessException() {
538+
assertStrictness(Metafix.Strictness.EXPRESSION, "upcase()", true, i -> i.setStrictnessHandlesProcessExceptions(true), o -> {
539+
o.get().startRecord("1");
540+
o.get().literal("data", "foo");
541+
o.get().literal("before", "");
542+
o.get().literal("after", "");
543+
o.get().endRecord();
544+
545+
o.get().startRecord("2");
546+
o.get().literal("data", "foo");
547+
o.get().literal("data", "bar");
548+
o.get().literal("before", "");
549+
o.get().literal("after", "");
550+
o.get().endRecord();
551+
552+
o.get().startRecord("3");
553+
o.get().literal("data", "bar");
554+
o.get().literal("before", "");
555+
o.get().literal("after", "");
556+
o.get().endRecord();
557+
});
558+
}
559+
532560
private void assertVar(final String fixDef, final Map<String, String> vars, final Map<String, String> result) {
533561
assertFix(fixDef, vars, f -> result.forEach((k, v) -> Assertions.assertEquals(v, f.getVars().get(k))));
534562
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"contribution" : [ {
3+
"agent" : {
4+
"label" : "COLEMAN, Julie"
5+
}
6+
} ]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<collection xmlns="http://www.loc.gov/MARC21/slim">
3+
<record>
4+
<datafield tag="700" ind1="1" ind2=" ">
5+
<subfield code="a">COLEMAN, Julie</subfield>
6+
<subfield code="e">Editor</subfield>
7+
<subfield code="a">KAY, Christian J.</subfield>
8+
<subfield code="e">NS</subfield>
9+
</datafield>
10+
</record>
11+
</collection>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
set_array("contribution[]")
2+
3+
do list(path: "700[01] ", "var": "$i")
4+
set_hash("contribution[].$append.agent")
5+
copy_field("$i.a", "contribution[].$last.agent.label")
6+
end
7+
8+
replace_all("contribution[].*.agent.label", "(?<!\\p{Upper})\\.$|[,]$", "")
9+
retain("contribution[]")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FLUX_DIR + "input.xml"
2+
|open-file
3+
|decode-xml
4+
|handle-marcxml
5+
|fix(FLUX_DIR + "test.fix", strictness="expression", strictnessHandlesProcessExceptions="true")
6+
|encode-json(prettyPrinting="true")
7+
|write(FLUX_DIR + "output-metafix.json")
8+
;

0 commit comments

Comments
 (0)