• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pkiraly / metadata-qa-marc / #1430

12 Mar 2025 12:23PM UTC coverage: 90.395% (-0.06%) from 90.457%
#1430

push

pkiraly
Merge branch 'main' of github.com:pkiraly/qa-catalogue

36517 of 40397 relevant lines covered (90.4%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.26
/src/main/java/de/gwdg/metadataqa/marc/cli/utils/ignorablerecords/Marc21Filter.java
1
package de.gwdg.metadataqa.marc.cli.utils.ignorablerecords;
2

3
import de.gwdg.metadataqa.marc.MarcSubfield;
4
import de.gwdg.metadataqa.marc.dao.DataField;
5
import de.gwdg.metadataqa.marc.dao.record.BibliographicRecord;
6
import org.apache.commons.lang3.StringUtils;
7

8
import java.util.ArrayList;
9
import java.util.List;
10
import java.util.regex.Pattern;
11

12
public class Marc21Filter {
1✔
13

14
  protected List<Condition> conditions;
15

16
  protected void parseInput(String input) {
17
    if (StringUtils.isNotBlank(input)) {
1✔
18
      conditions = new ArrayList<>();
1✔
19
      for (String field : input.split(",")) {
1✔
20
        Condition f = parseFieldCondition(field);
1✔
21
        if (f != null)
1✔
22
          conditions.add(f);
1✔
23
      }
24
    }
25
  }
1✔
26

27
  public List<Condition> getConditions() {
28
    return conditions;
1✔
29
  }
30

31
  public boolean isEmpty() {
32
    return conditions == null || conditions.isEmpty();
1✔
33
  }
34

35
  /**
36
   * Parses the given string field condition which is in a format of "tag$subfield=value" (e.g. "001$0=123456").
37
   * @param field The field condition to parse
38
   * @return The parsed field condition, which is essentially a new DataField object
39
   */
40
  protected Condition parseFieldCondition(String field) {
41
    var pattern = Pattern.compile(
1✔
42
      "^(.{3})(?:\\$(.))?\\s?(=~|!=|!~|==|=\\^|=\\$|\\?|=|not in|in)\\s?'?(.*?)'?$"
43
    );
44
    var matcher = pattern.matcher(field);
1✔
45
    if (matcher.matches()) {
1✔
46
      String tag = matcher.group(1);
1✔
47
      String subfield = matcher.group(2);
1✔
48
      String operator = matcher.group(3);
1✔
49
      String value = matcher.group(4);
1✔
50
      try {
51
        return new Condition(tag, subfield, Operator.byCode(operator), value);
1✔
52
      } catch (Exception e) {
×
53
        throw new RuntimeException(e);
×
54
      }
55
    }
56
    return null;
×
57
  }
58

59
  /**
60
   * Checks if the given record meets the set of criteria. If only one of the criteria is met, the method returns
61
   * true. If none of the criteria is met, the method returns false.
62
   * @param marcRecord The record to check
63
   * @return True if the record meets the criteria, false otherwise
64
   */
65
  protected boolean met(BibliographicRecord marcRecord) {
66
    for (Condition condition : conditions) {
1✔
67
      List<DataField> recordFields = marcRecord.getDatafieldsByTag(condition.getTag());
1✔
68
      if (recordFields == null || recordFields.isEmpty())
1✔
69
        continue;
×
70
      boolean passed = metCondition(condition, recordFields);
1✔
71
      if (passed)
1✔
72
        return true;
1✔
73
    }
×
74
    return false;
×
75
  }
76

77
  /**
78
   * Checks if the given fields meet the given condition. If the condition is met, the method returns true.
79
   * @param condition Condition to check the record for, for now in the form of a DataField object
80
   * @param recordFields Fields of the record to check
81
   * @return True if the fields meet the condition, false otherwise
82
   */
83
  private boolean metCondition(Condition condition, List<DataField> recordFields) {
84
    for (DataField recordField : recordFields) {
1✔
85
      String code = condition.getSubfield();
1✔
86
      List<MarcSubfield> recordSubfields = recordField.getSubfield(code);
1✔
87

88
      if (recordSubfields == null || recordSubfields.isEmpty())
1✔
89
        continue;
×
90

91
      for (MarcSubfield recordSubfield : recordSubfields) {
1✔
92
        if (condition.isUseEqual()) {
1✔
93
          if (recordSubfield.getValue().equals(condition.getValue()))
1✔
94
            return condition.isNegate() ? false : true;
1✔
95
        } else if (condition.isUsePattern()) {
1✔
96
          var matcher = condition.getPattern().matcher(recordSubfield.getValue());
1✔
97
          if (matcher.find())
1✔
98
            return condition.isNegate() ? false : true;
1✔
99
        }
100
      }
×
101
    }
×
102
    return false;
×
103
  }
104
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc