working on key based building of marc fields

This commit is contained in:
Jörg Prante 2022-10-31 18:43:16 +01:00
parent 28b9a2d86d
commit 5a644f4e41
3 changed files with 51 additions and 15 deletions

View file

@ -20,6 +20,7 @@ import org.xbib.marc.label.RecordLabel;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@ -533,10 +534,10 @@ public class MarcField implements Comparable<MarcField> {
/**
* A key is a compact representation of tag/indicator/value
* @param key
* @return
* @param key the key as string
* @return this builder
*/
public Builder key(String key, String separator, String value) {
public Builder key(String key, String separator, Object value) {
String[] s = key.split(separator);
switch (s.length) {
case 3: {
@ -547,11 +548,19 @@ public class MarcField implements Comparable<MarcField> {
} else {
indicator(indicator);
}
String subfieldId = s[2].replace('_', ' ');
if (subfieldId.isEmpty()) {
subfield(" ", value);
String subfieldIds = s[2].replace('_', ' ');
if (subfieldIds.isEmpty()) {
subfield(" ", value.toString());
} else {
subfield(subfieldId, value);
if (value instanceof List) {
List<Object> list = (List) value;
char[] ch = subfieldIds.toCharArray();
for (int i = 0; i < list.size(); i++) {
subfield(ch[i]).value(list.get(i).toString());
}
} else {
subfield(subfieldIds, value.toString());
}
}
break;
}
@ -563,17 +572,17 @@ public class MarcField implements Comparable<MarcField> {
} else {
indicator(indicator);
}
value(value);
value(value.toString());
break;
}
case 1: {
tag(s[0]);
value(value);
value(value.toString());
break;
}
case 0: {
tag(key);
value(value);
value(value.toString());
break;
}
default:

View file

@ -30,6 +30,8 @@ import java.util.Set;
import java.util.TreeMap;
import java.util.function.BiConsumer;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
/**
@ -97,8 +99,15 @@ public class MarcRecord implements Map<String, Object> {
String leaderTag,
RecordLabel recordLabel) {
MarcRecord marcRecord = new MarcRecord(map);
marcRecord.parseMap(map, ".", "", (key, value) ->
marcRecord.marcFields.add(MarcField.builder().key(key, "\\.", value.toString()).build()));
marcRecord.parseMap(map, ".", "", (key, value) -> {
if (value instanceof Collection<?>) {
MarcField.Builder builder = MarcField.builder().key(key);
((Collection<Map.Entry<String, Object>>) value).forEach(e -> builder.subfield(e.getKey(), e.getValue().toString()));
marcRecord.marcFields.add(builder.build());
} else {
marcRecord.marcFields.add(MarcField.builder().key(key, "\\.", value.toString()).build());
}
});
if (map.containsKey(formatTag)) {
marcRecord.format = map.get(formatTag).toString();
}
@ -350,19 +359,23 @@ public class MarcRecord implements Map<String, Object> {
}
@SuppressWarnings("unchecked")
private void parseMap(Map<String, Object> source, String separator, String prefix, BiConsumer<String, Object> consumer) {
private void parseMap(Map<String, Object> source, String separator, String prefix,
BiConsumer<String, Object> consumer) {
source.forEach((k, v) -> {
if (v instanceof Map) {
parseMap((Map<String, Object>) v, separator, prefix + k + separator, consumer);
} else if (v instanceof Collection) {
Collection<Object> collection = (Collection<Object>) v;
List<Map.Entry<String, Object>> list = new LinkedList<>();
for (Object object : collection) {
if (object instanceof Map) {
parseMap((Map<String, Object>) object, separator, prefix + k + separator, consumer);
parseMap((Map<String, Object>) object, separator, prefix + k + separator, (a,b) -> list.add(Map.entry(a,b)));
} else {
consumer.accept(prefix + k, object);
list.add(Map.entry(prefix + k, object));
}
}
Logger.getLogger("").log(Level.INFO, "list = " + list);
consumer.accept(prefix + k, list);
} else {
consumer.accept(prefix + k, v);
}

View file

@ -36,6 +36,8 @@ import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
public class MarcRecordTest {
@ -216,6 +218,18 @@ public class MarcRecordTest {
assertEquals("Hello World", marcRecord.getFields().stream().filter(m -> m.getTag().equals("100")).findFirst().get().getFirstSubfieldValue("a"));
}
@Test
public void testMarcRecordFromMapNested() {
Map<String, Object> map = Map.of("016", Map.of("7_", List.of(Map.of("2", "DE-101", "a", "010000151"), Map.of("2", "DE-600", "a", "23-1"))));
MarcRecord marcRecord = MarcRecord.from(map);
Logger.getLogger("").log(Level.INFO, "marcrecord = " + marcRecord);
Logger.getLogger("").log(Level.INFO, "marcrecord fields = " + marcRecord.getFields());
marcRecord.filter(f -> "016".equals(f.getTag()), f-> {
Logger.getLogger("").log(Level.INFO, "f = " + f);
});
}
@Test
public void testMarcRecordFromMapAsMap() throws IOException {
Map<String, Object> map = new TreeMap<>(Map.of("001", "123",