working on key based building of marc fields
This commit is contained in:
parent
28b9a2d86d
commit
5a644f4e41
3 changed files with 51 additions and 15 deletions
|
@ -20,6 +20,7 @@ import org.xbib.marc.label.RecordLabel;
|
||||||
|
|
||||||
import java.util.Deque;
|
import java.util.Deque;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.Collectors;
|
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
|
* A key is a compact representation of tag/indicator/value
|
||||||
* @param key
|
* @param key the key as string
|
||||||
* @return
|
* @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);
|
String[] s = key.split(separator);
|
||||||
switch (s.length) {
|
switch (s.length) {
|
||||||
case 3: {
|
case 3: {
|
||||||
|
@ -547,11 +548,19 @@ public class MarcField implements Comparable<MarcField> {
|
||||||
} else {
|
} else {
|
||||||
indicator(indicator);
|
indicator(indicator);
|
||||||
}
|
}
|
||||||
String subfieldId = s[2].replace('_', ' ');
|
String subfieldIds = s[2].replace('_', ' ');
|
||||||
if (subfieldId.isEmpty()) {
|
if (subfieldIds.isEmpty()) {
|
||||||
subfield(" ", value);
|
subfield(" ", value.toString());
|
||||||
} else {
|
} 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;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -563,17 +572,17 @@ public class MarcField implements Comparable<MarcField> {
|
||||||
} else {
|
} else {
|
||||||
indicator(indicator);
|
indicator(indicator);
|
||||||
}
|
}
|
||||||
value(value);
|
value(value.toString());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 1: {
|
case 1: {
|
||||||
tag(s[0]);
|
tag(s[0]);
|
||||||
value(value);
|
value(value.toString());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 0: {
|
case 0: {
|
||||||
tag(key);
|
tag(key);
|
||||||
value(value);
|
value(value.toString());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -30,6 +30,8 @@ import java.util.Set;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -97,8 +99,15 @@ public class MarcRecord implements Map<String, Object> {
|
||||||
String leaderTag,
|
String leaderTag,
|
||||||
RecordLabel recordLabel) {
|
RecordLabel recordLabel) {
|
||||||
MarcRecord marcRecord = new MarcRecord(map);
|
MarcRecord marcRecord = new MarcRecord(map);
|
||||||
marcRecord.parseMap(map, ".", "", (key, value) ->
|
marcRecord.parseMap(map, ".", "", (key, value) -> {
|
||||||
marcRecord.marcFields.add(MarcField.builder().key(key, "\\.", value.toString()).build()));
|
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)) {
|
if (map.containsKey(formatTag)) {
|
||||||
marcRecord.format = map.get(formatTag).toString();
|
marcRecord.format = map.get(formatTag).toString();
|
||||||
}
|
}
|
||||||
|
@ -350,19 +359,23 @@ public class MarcRecord implements Map<String, Object> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@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) -> {
|
source.forEach((k, v) -> {
|
||||||
if (v instanceof Map) {
|
if (v instanceof Map) {
|
||||||
parseMap((Map<String, Object>) v, separator, prefix + k + separator, consumer);
|
parseMap((Map<String, Object>) v, separator, prefix + k + separator, consumer);
|
||||||
} else if (v instanceof Collection) {
|
} else if (v instanceof Collection) {
|
||||||
Collection<Object> collection = (Collection<Object>) v;
|
Collection<Object> collection = (Collection<Object>) v;
|
||||||
|
List<Map.Entry<String, Object>> list = new LinkedList<>();
|
||||||
for (Object object : collection) {
|
for (Object object : collection) {
|
||||||
if (object instanceof Map) {
|
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 {
|
} 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 {
|
} else {
|
||||||
consumer.accept(prefix + k, v);
|
consumer.accept(prefix + k, v);
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,6 +36,8 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class MarcRecordTest {
|
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"));
|
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
|
@Test
|
||||||
public void testMarcRecordFromMapAsMap() throws IOException {
|
public void testMarcRecordFromMapAsMap() throws IOException {
|
||||||
Map<String, Object> map = new TreeMap<>(Map.of("001", "123",
|
Map<String, Object> map = new TreeMap<>(Map.of("001", "123",
|
||||||
|
|
Loading…
Reference in a new issue