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"
group = "org.xbib"
version = "1.0.5"
version = "1.0.6"
configurations {
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.
* 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() {
return (tag == null ? EMPTY_STRING : tag) + KEY_DELIMITER + (indicator == null ? EMPTY_STRING : indicator) +
KEY_DELIMITER + subfieldIds;
public String toTagKey() {
return tag == null ? EMPTY_STRING : tag;
}
/**
@ -244,13 +243,22 @@ public class MarcField implements Comparable<MarcField> {
}
/**
* A MARC field can be denoted by a key, independent of values.
* This key is a string, consisting of the tag.
*
* @return the tag-based key of this MARC field
* Return subfield IDs.
* @return the subfield ID list as a string.
*/
public String toTagKey() {
return tag == null ? EMPTY_STRING : tag;
public String getSubfieldIds() {
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

View file

@ -27,6 +27,8 @@ public class MarcValueTransformers {
private final Map<String, MarcValueTransformer> marcValueTransformerMap = new HashMap<>();
private final Map<String, String> subfieldMap = new HashMap<>();
public MarcValueTransformers setMarcValueTransformer(MarcValueTransformer transformer) {
this.marcValueTransformerMap.put("_default", transformer);
return this;
@ -39,7 +41,13 @@ public class MarcValueTransformers {
* @return this handler
*/
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;
}
@ -49,7 +57,7 @@ public class MarcValueTransformers {
* @return a new MARC field with transformed values
*/
public MarcField transformValue(MarcField field) {
String key = field.toKey();
String key = field.toTagIndicatorKey();
if (marcValueTransformerMap.isEmpty()) {
return field;
}
@ -61,8 +69,11 @@ public class MarcValueTransformers {
if (field.getValue() != null) {
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 ->
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 field;

View file

@ -14,12 +14,7 @@ public class MarcValueTransformerTest {
@Test
public void testValueTransformer() {
MarcValueTransformer marcValueTransformer = new MarcValueTransformer() {
@Override
public String transform(String value) {
return value.equals("World") ? "Earth" : value;
}
};
MarcValueTransformer marcValueTransformer = value -> value.equals("World") ? "Earth" : value;
MarcValueTransformers marcValueTransformers = new MarcValueTransformers();
marcValueTransformers.setMarcValueTransformer(marcValueTransformer);
MarcField a = MarcField.builder().tag("100").subfield("a", "Hello").subfield("b", "World").build();
@ -29,12 +24,7 @@ public class MarcValueTransformerTest {
@Test
public void testValueControlFieldTransformer() {
MarcValueTransformer marcValueTransformer = new MarcValueTransformer() {
@Override
public String transform(String value) {
return value.equals("World") ? "Earth" : value;
}
};
MarcValueTransformer marcValueTransformer = value -> value.equals("World") ? "Earth" : value;
MarcValueTransformers marcValueTransformers = new MarcValueTransformers();
marcValueTransformers.setMarcValueTransformer(marcValueTransformer);
MarcField a = MarcField.builder().tag("001").value("World").build();
@ -42,4 +32,29 @@ public class MarcValueTransformerTest {
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());
}
}