use a list as keys for marc records from map

This commit is contained in:
Jörg Prante 2022-11-01 12:58:36 +01:00
parent 4ff083db34
commit efcf60d602
3 changed files with 50 additions and 29 deletions

View file

@ -52,8 +52,8 @@ if (project.hasProperty("signing.keyId")) {
sign publishing.publications.mavenJava
}
}
nexusPublishing {
if (project.hasProperty("ossrhUsername")) {
nexusPublishing {
repositories {
sonatype {
username = project.property('ossrhUsername')
@ -61,4 +61,5 @@ nexusPublishing {
packageGroup = "org.xbib"
}
}
}
}

View file

@ -18,9 +18,11 @@ package org.xbib.marc;
import org.xbib.marc.dialects.mab.MabSubfieldControl;
import org.xbib.marc.label.RecordLabel;
import java.util.Arrays;
import java.util.Collection;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@ -534,22 +536,30 @@ 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 that are separated by a separator.
* @param key the key as string
* @param separator the separator in the key
* @param value the value
* @return this builder
*/
@SuppressWarnings("unchecked")
public Builder key(String key, String separator, Object value) {
String[] s = key.split(separator);
switch (s.length) {
return key(Arrays.asList(key.split(separator)), value);
}
@SuppressWarnings("unchecked")
public Builder key(List<String> key, Object value) {
switch (key.size()) {
case 0: {
// is that even possible?
tag(key);
value(value.toString());
if (value instanceof Collection) {
Collection<Map.Entry<String, Object>> collection = (Collection<Map.Entry<String, Object>>) value;
for (Map.Entry<String, Object> entry : collection) {
tag(entry.getKey()).value(entry.getValue().toString());
}
}
break;
}
case 1: {
tag(s[0]);
tag(key.get(0));
if (value instanceof Collection) {
Collection<Map.Entry<String, Object>> collection = (Collection<Map.Entry<String, Object>>) value;
for (Map.Entry<String, Object> entry : collection) {
@ -561,8 +571,8 @@ public class MarcField implements Comparable<MarcField> {
break;
}
case 2: {
tag(s[0]);
String indicator = s[1].replace('_', ' ');
tag(key.get(0));
String indicator = key.get(1).replace('_', ' ');
if (indicator.isEmpty()) {
indicator(" ");
} else {
@ -579,14 +589,14 @@ public class MarcField implements Comparable<MarcField> {
break;
}
case 3: {
tag(s[0]);
String indicator = s[1].replace('_', ' ');
tag(key.get(0));
String indicator = key.get(1).replace('_', ' ');
if (indicator.isEmpty()) {
indicator(" ");
} else {
indicator(indicator);
}
String subfieldIds = s[2].replace('_', ' ');
String subfieldIds = key.get(2).replace('_', ' ');
if (subfieldIds.isEmpty()) {
subfieldIds = " ";
}
@ -601,7 +611,7 @@ public class MarcField implements Comparable<MarcField> {
break;
}
default:
throw new IllegalArgumentException("key specification is invalid: " + key + " length = " + s.length);
throw new IllegalArgumentException("key specification is invalid: " + key);
}
return this;
}

View file

@ -22,6 +22,7 @@ import static org.xbib.marc.json.MarcJsonWriter.TYPE_TAG;
import org.xbib.marc.label.RecordLabel;
import java.util.Collection;
import java.util.Deque;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
@ -30,6 +31,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,12 +100,12 @@ 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).build()));
marcRecord.parseMap(map, "", new LinkedList<>(), (key, value) ->
marcRecord.marcFields.add(MarcField.builder().key(key, value).build()));
if (map.containsKey(formatTag)) {
marcRecord.format = map.get(formatTag).toString();
}
if ( map.containsKey(typeTag)) {
if (map.containsKey(typeTag)) {
marcRecord.type = map.get(typeTag).toString();
}
if (map.containsKey(leaderTag)) {
@ -478,17 +481,21 @@ public class MarcRecord implements Map<String, Object> {
@SuppressWarnings("unchecked")
private void parseMap(Map<String, Object> source,
String separator, String prefix,
BiConsumer<String, Object> consumer) {
String prefix,
LinkedList<String> key,
BiConsumer<List<String>, Object> consumer) {
if (!prefix.isEmpty()) {
key.addLast(prefix);
}
List<Map.Entry<String, Object>> list = new LinkedList<>();
source.forEach((k, v) -> {
if (v instanceof Map) {
parseMap((Map<String, Object>) v, separator, prefix + k + separator, consumer);
parseMap((Map<String, Object>) v, k, key, consumer);
} else if (v instanceof Collection) {
Collection<Object> collection = (Collection<Object>) v;
for (Object object : collection) {
if (object instanceof Map) {
parseMap((Map<String, Object>) object, separator, prefix + k + separator, consumer);
parseMap((Map<String, Object>) object, k, key, consumer);
} else {
list.add(Map.entry(k, object));
}
@ -498,7 +505,10 @@ public class MarcRecord implements Map<String, Object> {
}
});
if (!list.isEmpty()) {
consumer.accept(prefix, list);
consumer.accept(key, list);
}
if (!prefix.isEmpty()) {
key.removeLast();
}
}
}