Skip to content

Commit

Permalink
First steps to convert MARC schema to Avram format
Browse files Browse the repository at this point in the history
As listed at <timathom/marc-schema#1>:

- wrap the list of fields in a root object with key 'fields' and add key family with value 'marc'
- rename key 'name' to 'label'
- rename key 'values' to 'codes'
- remove key 'fixed', its value is implied by non-existence of 'subfields'
  • Loading branch information
nichtich committed Nov 24, 2023
1 parent 29fa7ca commit 33bb53d
Show file tree
Hide file tree
Showing 2 changed files with 12,278 additions and 12,419 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
public class Marc21SchemaReader {
private static final Logger logger = Logger.getLogger(Marc21SchemaReader.class.getCanonicalName());
private static final Map<String, Integer> knownFieldProperties = Map.of(
"name", 1, "repeatable", 1, "fixed", 1, "indicators", 1, "subfields", 1, "positions", 1);
"label", 1, "repeatable", 1, "indicators", 1, "subfields", 1, "positions", 1);
private static final Map<String, Integer> knownSubfieldProperties = Map.of(
"name", 1, "repeatable", 1, "static", 1, "staticValues", 1);
"label", 1, "repeatable", 1, "static", 1, "staticValues", 1);
private static final Map<String, Integer> knownSubfieldCodeProperties = Map.of(
"name", 1, "value",1);
"label", 1, "value",1);
private static final Map<String, Integer> knownIndicatorProperties = Map.of(
"name", 1, "values",1);
"label", 1, "codes",1);
private JSONParser parser = new JSONParser(JSONParser.MODE_RFC4627);
private Map<String, DataFieldDefinition> map = new HashMap<>();

Expand Down Expand Up @@ -64,17 +64,18 @@ private void readStream(InputStream inputStream) throws IOException, ParseExcept
}

private void process(JSONObject jsonObject) throws IOException, ParseException, URISyntaxException {
for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
JSONObject fields = (JSONObject) jsonObject.get("fields");
for (Map.Entry<String, Object> entry : fields.entrySet()) {
String id = entry.getKey();
// if (id.equals("leader"))
// continue;

JSONObject field = (JSONObject) entry.getValue();
DataFieldDefinition tag = new Marc21DataFieldDefinition(
id,
(String) field.get("name"),
(String) field.get("label"),
(boolean) field.get("repeatable"),
(boolean) field.get("fixed")
!field.containsKey("subfields")
);
if (field.containsKey("indicators"))
processIndicators((JSONObject) field.get("indicators"), (Marc21DataFieldDefinition) tag);
Expand Down Expand Up @@ -107,11 +108,11 @@ private void processPositions(JSONArray positions, Marc21DataFieldDefinition tag
private ControlfieldPositionDefinition processPosition(Object positionNode, Marc21DataFieldDefinition tag) {
if (positionNode instanceof JSONObject) {
JSONObject position = (JSONObject) positionNode;
String label = position.getAsString("name");
String label = position.getAsString("label");
int start = (int) position.getAsNumber("start");
int end = (int) position.getAsNumber("stop") + 1;
ControlfieldPositionDefinition definition = new ControlfieldPositionDefinition(label, start, end);
if (position.containsKey("values")) {
if (position.containsKey("codes")) {
definition.setCodes(extractEncodedValues(position));
int lenght = (end - start);
if (lenght > 1
Expand Down Expand Up @@ -145,8 +146,8 @@ else if (key.equals("2"))
private Indicator processIndicator(Object indicatorNode, String tag) {
if (indicatorNode instanceof JSONObject) {
JSONObject indicator = (JSONObject) indicatorNode;
Indicator definition = new Indicator((String)indicator.get("name"));
if (indicator.containsKey("values")) {
Indicator definition = new Indicator((String)indicator.get("label"));
if (indicator.containsKey("codes")) {
definition.setCodes(extractEncodedValues(indicator));
}
for (String key : indicator.keySet())
Expand All @@ -162,7 +163,7 @@ private Indicator processIndicator(Object indicatorNode, String tag) {

private static List<EncodedValue> extractEncodedValues(JSONObject valueContainer) {
List<EncodedValue> codes = new LinkedList<>();
for (Map.Entry<String, Object> value : ((JSONObject) valueContainer.get("values")).entrySet()) {
for (Map.Entry<String, Object> value : ((JSONObject) valueContainer.get("codes")).entrySet()) {
String code = value.getKey().equals("#") ? " " : value.getKey();
codes.add(new EncodedValue(code, (String) value.getValue()));
}
Expand All @@ -189,7 +190,7 @@ private SubfieldDefinition extractSubfield(String code, Object o) {
SubfieldDefinition definition = null;
if (o instanceof JSONObject) {
JSONObject subfield = (JSONObject) o;
String label = (String) subfield.get("name");
String label = (String) subfield.get("label");
boolean repeatable = (boolean) subfield.get("repeatable");
String cardinalityCode = repeatable ? Cardinality.Repeatable.getCode() : Cardinality.Nonrepeatable.getCode();
definition = new SubfieldDefinition(code, label, cardinalityCode);
Expand All @@ -215,7 +216,7 @@ private List<EncodedValue> processStaticValues(Object staticValuesObject) {
if (!code.equals(property.get("value")))
logger.warning(String.format("code is different to code value: %s vs %s", code, (String)property.get("value")));
else
list.add(new EncodedValue(code, (String) property.get("name")));
list.add(new EncodedValue(code, (String) property.get("label")));
for (String key : property.keySet()) {
if (!knownSubfieldCodeProperties.containsKey(key))
logger.warning("unhandled subfield code property: " + key);
Expand Down
Loading

0 comments on commit 33bb53d

Please sign in to comment.