Skip to content

Commit 876be2e

Browse files
committed
Add option to set record IDs in ObjectToLiteral
See #353
1 parent b7e78ea commit 876be2e

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

metafacture-mangling/src/main/java/org/metafacture/mangling/ObjectToLiteral.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013, 2014 Deutsche Nationalbibliothek
2+
* Copyright 2013, 2021 Deutsche Nationalbibliothek and others
33
*
44
* Licensed under the Apache License, Version 2.0 the "License";
55
* you may not use this file except in compliance with the License.
@@ -25,7 +25,7 @@
2525
* Outputs a record containing the input object as literal.
2626
*
2727
* @param <T> input object type
28-
* @author Christoph Böhme
28+
* @author Christoph Böhme, Fabian Steeg
2929
*/
3030
@Description("Outputs a record containing the input object as literal")
3131
@Out(StreamReceiver.class)
@@ -34,22 +34,40 @@ public final class ObjectToLiteral<T> extends
3434
DefaultObjectPipe<T, StreamReceiver> {
3535

3636
public static final String DEFAULT_LITERAL_NAME = "obj";
37+
public static final String DEFAULT_RECORD_ID = "";
3738

38-
private String literalName = DEFAULT_LITERAL_NAME;
39+
private String literalName;
40+
private String recordId;
41+
private int recordCount;
42+
43+
public ObjectToLiteral() {
44+
super();
45+
setLiteralName(DEFAULT_LITERAL_NAME);
46+
setRecordId(DEFAULT_RECORD_ID);
47+
recordCount=0;
48+
}
3949

4050
public void setLiteralName(final String literalName) {
4151
this.literalName = literalName;
4252
}
4353

54+
public void setRecordId(final String recordId) {
55+
this.recordId = recordId;
56+
}
57+
4458
public String getLiteralName() {
4559
return literalName;
4660
}
4761

62+
public String getRecordId() {
63+
return recordId;
64+
}
65+
4866
@Override
4967
public void process(final T obj) {
5068
assert obj!=null;
5169
assert !isClosed();
52-
getReceiver().startRecord("");
70+
getReceiver().startRecord(String.format(recordId, ++recordCount));
5371
getReceiver().literal(literalName, obj.toString());
5472
getReceiver().endRecord();
5573
}

metafacture-mangling/src/test/java/org/metafacture/mangling/ObjectToLiteralTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,20 @@ public void testShouldUseCustomLiteralName() {
7777
ordered.verify(receiver).endRecord();
7878
}
7979

80+
@Test
81+
public void testShouldSetRecordIds() {
82+
objectToLiteral.setRecordId("Record %d");
83+
84+
objectToLiteral.process(OBJ_DATA);
85+
objectToLiteral.process(OBJ_DATA);
86+
87+
final InOrder ordered = inOrder(receiver);
88+
ordered.verify(receiver).startRecord("Record 1");
89+
ordered.verify(receiver).literal(ObjectToLiteral.DEFAULT_LITERAL_NAME, OBJ_DATA);
90+
ordered.verify(receiver).endRecord();
91+
ordered.verify(receiver).startRecord("Record 2");
92+
ordered.verify(receiver).literal(ObjectToLiteral.DEFAULT_LITERAL_NAME, OBJ_DATA);
93+
ordered.verify(receiver).endRecord();
94+
}
95+
8096
}

0 commit comments

Comments
 (0)