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,13 +52,14 @@ if (project.hasProperty("signing.keyId")) {
sign publishing.publications.mavenJava sign publishing.publications.mavenJava
} }
} }
if (project.hasProperty("ossrhUsername")) {
nexusPublishing { nexusPublishing {
repositories { repositories {
sonatype { sonatype {
username = project.property('ossrhUsername') username = project.property('ossrhUsername')
password = project.property('ossrhPassword') password = project.property('ossrhPassword')
packageGroup = "org.xbib" packageGroup = "org.xbib"
}
} }
} }
} }

View file

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

View file

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