Skip to content

Commit 76eb9fd

Browse files
committed
Add setting to emit leader as a whole
As a "leader" field is possible in marc21 this makes it possible to access the whole leader, thus enhancing the already possible access of bibliographically relevant part of it. See #300.
1 parent 78aaca3 commit 76eb9fd

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-9
lines changed

metafacture-biblio/src/main/java/org/metafacture/biblio/iso2709/Record.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ public String getRecordId() {
156156
return buffer.stringAt(dataStart, dataLength, charset);
157157
}
158158

159+
/**
160+
* Returns the record leader.
161+
*
162+
* @return a string which is the record leader.
163+
*/
164+
public String getLabel() {
165+
return label.toString();
166+
}
159167
/**
160168
* Iterates through all fields in the record and calls the appropriate method
161169
* on the supplied {@link FieldHandler} instance.

metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/Marc21Decoder.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ public final class Marc21Decoder
140140
private final FieldHandler fieldHandler = new Marc21Handler();
141141

142142
private boolean ignoreMissingId;
143+
private boolean emitLeaderAsWhole;
143144

144145
/**
145146
* Controls whether the decoder aborts processing if a record has no
@@ -164,6 +165,24 @@ public boolean getIgnoreMissingId() {
164165
return ignoreMissingId;
165166
}
166167

168+
/**
169+
* Controls whether the Record Leader should be emitted as a whole instead of
170+
* extracting the bibliographic information in the record leader.
171+
*
172+
* @see <a href="http://www.loc.gov/marc/bibliographic/bdleader.html">MARC 21
173+
* Standard: Record Leader</a>
174+
*
175+
* @param emitLeaderAsWhole
176+
* true if the leader should be emitted as a whole.
177+
*/
178+
public void setEmitLeaderAsWhole(final boolean emitLeaderAsWhole) {
179+
this.emitLeaderAsWhole = emitLeaderAsWhole;
180+
}
181+
182+
public boolean getEmitLeaderAsWhole() {
183+
return emitLeaderAsWhole;
184+
}
185+
167186
@Override
168187
public void process(final String obj) {
169188
if (obj.isEmpty()) {
@@ -207,9 +226,12 @@ private String tryGetRecordId(final Record record) {
207226
}
208227

209228
private void emitLeader(final Record record) {
229+
getReceiver().startEntity(Marc21EventNames.LEADER_ENTITY);
230+
if (emitLeaderAsWhole){
231+
getReceiver().literal(Marc21EventNames.LEADER_ENTITY, record.getLabel());
232+
}else {
210233
final char[] implCodes = record.getImplCodes();
211234
final char[] systemChars = record.getSystemChars();
212-
getReceiver().startEntity(Marc21EventNames.LEADER_ENTITY);
213235
getReceiver().literal(Marc21EventNames.RECORD_STATUS_LITERAL, String.valueOf(
214236
record.getRecordStatus()));
215237
getReceiver().literal(Marc21EventNames.RECORD_TYPE_LITERAL, String.valueOf(
@@ -226,6 +248,7 @@ private void emitLeader(final Record record) {
226248
systemChars[Marc21Constants.CATALOGING_FORM_INDEX]));
227249
getReceiver().literal(Marc21EventNames.MULTIPART_LEVEL_LITERAL, String.valueOf(
228250
systemChars[Marc21Constants.MULTIPART_LEVEL_INDEX]));
251+
}
229252
getReceiver().endEntity();
230253
}
231254

metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/MarcXmlEncoder.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public final class MarcXmlEncoder extends DefaultStreamPipe<ObjectReceiver<Strin
3838
private static final String SUBFIELD_OPEN_TEMPLATE = "<marc:subfield code=\"%s\">";
3939
private static final String SUBFIELD_CLOSE = "</marc:subfield>";
4040

41+
private static final String LEADER_OPEN_TEMPLATE = "<marc:leader>";
42+
private static final String LEADER_CLOSE_TEMPLATE = "</marc:leader>";
43+
4144
private static final String NEW_LINE = "\n";
4245
private static final String INDENT = "\t";
4346

@@ -152,25 +155,27 @@ public void endEntity() {
152155
}
153156

154157
@Override
155-
public void literal(final String name, final String value)
156-
{
157-
if (currentEntity.equals(""))
158-
{
158+
public void literal(final String name, final String value) {
159+
if (currentEntity.equals("")) {
159160
prettyPrintIndentation();
160161
writeRaw(String.format(CONTROLFIELD_OPEN_TEMPLATE, name));
161162
writeEscaped(value.trim());
162163
writeRaw(CONTROLFIELD_CLOSE);
163164
prettyPrintNewLine();
164-
}
165-
else if (!currentEntity.equals("leader"))
166-
{
165+
} else if (!currentEntity.equals("leader")) {
167166
prettyPrintIndentation();
168167
writeRaw(String.format(SUBFIELD_OPEN_TEMPLATE, name));
169168
writeEscaped(value.trim());
170169
writeRaw(SUBFIELD_CLOSE);
171170
prettyPrintNewLine();
171+
} else {
172+
if (name.equals(Marc21EventNames.LEADER_ENTITY)) {
173+
prettyPrintIndentation();
174+
writeRaw(LEADER_OPEN_TEMPLATE + value + LEADER_CLOSE_TEMPLATE);
175+
prettyPrintNewLine();
176+
}
172177
}
173-
else {
178+
174179
}
175180

176181
@Override

metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/MarcXmlEncoderTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,17 @@ public void createTwoRecordsInOneCollection() throws Exception {
111111

112112
assertEquals(expected, actual);
113113
}
114+
115+
@Test
116+
public void createAnRecordWithLeader() throws Exception {
117+
encoder.startRecord("1");
118+
encoder.startEntity("leader");
119+
encoder.literal("leader", "dummy");
120+
encoder.endEntity();
121+
encoder.endRecord();
122+
encoder.closeStream();
123+
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><marc:collection xmlns:marc=\"http://www.loc.gov/MARC21/slim\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd\"><marc:record><marc:leader>dummy</marc:leader></marc:record></marc:collection>";
124+
String actual = resultCollector.toString();
125+
assertEquals(expected, actual);
126+
}
114127
}

0 commit comments

Comments
 (0)