allow single subfield value transformation

This commit is contained in:
Jörg Prante 2016-11-25 20:45:30 +01:00
parent 0f46ffb6a9
commit 8b0cb74ccd
4 changed files with 61 additions and 27 deletions

View file

@ -13,7 +13,7 @@ apply plugin: 'checkstyle'
apply plugin: "jacoco" apply plugin: "jacoco"
group = "org.xbib" group = "org.xbib"
version = "1.0.5" version = "1.0.6"
configurations { configurations {
wagon wagon

View file

@ -224,13 +224,12 @@ public class MarcField implements Comparable<MarcField> {
/** /**
* A MARC field can be denoted by a key, independent of values. * A MARC field can be denoted by a key, independent of values.
* This key is a string, consisting of tag, indicator, subfield IDs, delimited by a dollar sign. * This key is a string, consisting of the tag.
* *
* @return the key of this MARC field * @return the tag-based key of this MARC field
*/ */
public String toKey() { public String toTagKey() {
return (tag == null ? EMPTY_STRING : tag) + KEY_DELIMITER + (indicator == null ? EMPTY_STRING : indicator) + return tag == null ? EMPTY_STRING : tag;
KEY_DELIMITER + subfieldIds;
} }
/** /**
@ -244,13 +243,22 @@ public class MarcField implements Comparable<MarcField> {
} }
/** /**
* A MARC field can be denoted by a key, independent of values. * Return subfield IDs.
* This key is a string, consisting of the tag. * @return the subfield ID list as a string.
*
* @return the tag-based key of this MARC field
*/ */
public String toTagKey() { public String getSubfieldIds() {
return tag == null ? EMPTY_STRING : tag; return subfieldIds;
}
/**
* A MARC field can be denoted by a key, independent of values.
* This key is a string, consisting of tag, indicator, subfield IDs, delimited by a dollar sign.
*
* @return the key of this MARC field
*/
public String toKey() {
return (tag == null ? EMPTY_STRING : tag) + KEY_DELIMITER + (indicator == null ? EMPTY_STRING : indicator) +
KEY_DELIMITER + subfieldIds;
} }
@Override @Override

View file

@ -27,6 +27,8 @@ public class MarcValueTransformers {
private final Map<String, MarcValueTransformer> marcValueTransformerMap = new HashMap<>(); private final Map<String, MarcValueTransformer> marcValueTransformerMap = new HashMap<>();
private final Map<String, String> subfieldMap = new HashMap<>();
public MarcValueTransformers setMarcValueTransformer(MarcValueTransformer transformer) { public MarcValueTransformers setMarcValueTransformer(MarcValueTransformer transformer) {
this.marcValueTransformerMap.put("_default", transformer); this.marcValueTransformerMap.put("_default", transformer);
return this; return this;
@ -39,7 +41,13 @@ public class MarcValueTransformers {
* @return this handler * @return this handler
*/ */
public MarcValueTransformers setMarcValueTransformer(String fieldKey, MarcValueTransformer transformer) { public MarcValueTransformers setMarcValueTransformer(String fieldKey, MarcValueTransformer transformer) {
this.marcValueTransformerMap.put(fieldKey, transformer); // split into tag + indicator and subfield IDs
int pos = fieldKey.lastIndexOf('$');
String tagind = pos > 0 ? fieldKey.substring(0, pos) : fieldKey;
String subs = pos > 0 ? fieldKey.substring(pos + 1) : "";
this.marcValueTransformerMap.put(tagind, transformer);
// subfield IDs
this.subfieldMap.put(tagind, subs);
return this; return this;
} }
@ -49,7 +57,7 @@ public class MarcValueTransformers {
* @return a new MARC field with transformed values * @return a new MARC field with transformed values
*/ */
public MarcField transformValue(MarcField field) { public MarcField transformValue(MarcField field) {
String key = field.toKey(); String key = field.toTagIndicatorKey();
if (marcValueTransformerMap.isEmpty()) { if (marcValueTransformerMap.isEmpty()) {
return field; return field;
} }
@ -61,8 +69,11 @@ public class MarcValueTransformers {
if (field.getValue() != null) { if (field.getValue() != null) {
builder.value(transformer.transform(field.getValue())); builder.value(transformer.transform(field.getValue()));
} }
// select only subfields configured for this tag
String subs = subfieldMap.containsKey(key) ? subfieldMap.get(key) : field.getSubfieldIds();
field.getSubfields().forEach(subfield -> field.getSubfields().forEach(subfield ->
builder.subfield(subfield.getId(), transformer.transform(subfield.getValue()))); builder.subfield(subfield.getId(), subs.contains(subfield.getId()) ?
transformer.transform(subfield.getValue()) : subfield.getValue()));
return builder.build(); return builder.build();
} }
return field; return field;

View file

@ -14,12 +14,7 @@ public class MarcValueTransformerTest {
@Test @Test
public void testValueTransformer() { public void testValueTransformer() {
MarcValueTransformer marcValueTransformer = new MarcValueTransformer() { MarcValueTransformer marcValueTransformer = value -> value.equals("World") ? "Earth" : value;
@Override
public String transform(String value) {
return value.equals("World") ? "Earth" : value;
}
};
MarcValueTransformers marcValueTransformers = new MarcValueTransformers(); MarcValueTransformers marcValueTransformers = new MarcValueTransformers();
marcValueTransformers.setMarcValueTransformer(marcValueTransformer); marcValueTransformers.setMarcValueTransformer(marcValueTransformer);
MarcField a = MarcField.builder().tag("100").subfield("a", "Hello").subfield("b", "World").build(); MarcField a = MarcField.builder().tag("100").subfield("a", "Hello").subfield("b", "World").build();
@ -29,12 +24,7 @@ public class MarcValueTransformerTest {
@Test @Test
public void testValueControlFieldTransformer() { public void testValueControlFieldTransformer() {
MarcValueTransformer marcValueTransformer = new MarcValueTransformer() { MarcValueTransformer marcValueTransformer = value -> value.equals("World") ? "Earth" : value;
@Override
public String transform(String value) {
return value.equals("World") ? "Earth" : value;
}
};
MarcValueTransformers marcValueTransformers = new MarcValueTransformers(); MarcValueTransformers marcValueTransformers = new MarcValueTransformers();
marcValueTransformers.setMarcValueTransformer(marcValueTransformer); marcValueTransformers.setMarcValueTransformer(marcValueTransformer);
MarcField a = MarcField.builder().tag("001").value("World").build(); MarcField a = MarcField.builder().tag("001").value("World").build();
@ -42,4 +32,29 @@ public class MarcValueTransformerTest {
assertEquals("001$$Earth", b.toString()); assertEquals("001$$Earth", b.toString());
} }
@Test
public void testValueTransformerForSingleSubfield() {
MarcValueTransformer marcValueTransformer = value -> value.equals("World") ? "Earth" : value;
MarcValueTransformers marcValueTransformers = new MarcValueTransformers();
// select only subfield 'b' for transformation
marcValueTransformers.setMarcValueTransformer("100$$b", marcValueTransformer);
// create MARC field with two subfields
MarcField a = MarcField.builder().tag("100").subfield("a", "World").subfield("b", "World").build();
MarcField b = marcValueTransformers.transformValue(a);
// check that transformation has been applied only to subfield 'b'
assertEquals("100$$ab[a=World, b=Earth]", b.toString());
}
@Test
public void testValueTransformerForManySubfields() {
MarcValueTransformer marcValueTransformer = value -> value.equals("World") ? "Earth" : value;
MarcValueTransformers marcValueTransformers = new MarcValueTransformers();
// select subfield 'a' and 'b' for transformation
marcValueTransformers.setMarcValueTransformer("100$$ab", marcValueTransformer);
// create MARC field with two subfields
MarcField a = MarcField.builder().tag("100").subfield("a", "World").subfield("b", "World").build();
MarcField b = marcValueTransformers.transformValue(a);
// check that transformation has been applied to both subfields
assertEquals("100$$ab[a=Earth, b=Earth]", b.toString());
}
} }