use of org.marc4j.marc.DataField in project RecordManager2 by moravianlibrary.
the class AdresarKnihovenMarcFunctions method adresarGetEmailOrMvs.
public List<String> adresarGetEmailOrMvs(MarcFunctionContext ctx, String tag) {
List<String> results = new ArrayList<>();
Matcher matcher = FIELD_PATTERN.matcher(tag);
if (!matcher.matches()) {
throw new IllegalArgumentException("Tag can't be parsed: " + tag);
}
String fieldTag = matcher.group(1);
String subFields = matcher.group(2);
for (DataField df : ctx.record().getDataFields(fieldTag)) {
StringBuilder sb = new StringBuilder();
if (df.getSubfield(subFields.charAt(0)) != null) {
sb.append(df.getSubfield(subFields.charAt(0)).getData());
}
if (df.getSubfield(subFields.charAt(1)) != null) {
sb.append(" (");
sb.append(df.getSubfield(subFields.charAt(1)).getData());
sb.append(")");
}
if (!sb.toString().isEmpty())
results.add(sb.toString().trim());
}
return results;
}
use of org.marc4j.marc.DataField in project RecordManager2 by moravianlibrary.
the class AdresarKnihovenMarcFunctions method adresarGetAddress.
public List<String> adresarGetAddress(MarcFunctionContext ctx) {
List<String> results = new ArrayList<>();
char[] sfCodes = new char[] { 'u', 'c', 'm', 'p' };
Subfield sf;
for (DataField df : ctx.record().getDataFields("ADR")) {
StringBuilder sb = new StringBuilder();
boolean isData = false;
for (int i = 0; i < sfCodes.length; i++) {
if ((sf = df.getSubfield(sfCodes[i])) == null)
continue;
switch(sf.getCode()) {
case 'c':
if (isData)
sb.append(", ");
sb.append(sf.getData());
break;
case 'm':
if (isData)
sb.append(" ");
sb.append(sf.getData());
break;
case 'p':
if (isData)
sb.append(" ");
sb.append("(");
sb.append(sf.getData());
sb.append(")");
break;
default:
sb.append(sf.getData());
break;
}
isData = true;
}
results.add(sb.toString().trim());
}
return results;
}
use of org.marc4j.marc.DataField in project RecordManager2 by moravianlibrary.
the class PublishDateMarcFunctions method getPublishDateFromFields.
public Set<String> getPublishDateFromFields(MarcFunctionContext ctx) {
MarcRecord record = ctx.record();
Set<String> years = new TreeSet<>();
for (DataField datafield : record.getDataFields("264")) {
if (datafield.getIndicator2() == '1') {
if (datafield.getSubfield('c') != null) {
years.add(datafield.getSubfield('c').getData());
}
}
}
years.addAll(record.getFields("260", 'c'));
years.addAll(record.getFields("773", '9'));
years.addAll(record.getFields("996", 'y'));
Set<String> results = new TreeSet<>();
years.forEach(y -> {
Matcher matcher;
if ((matcher = FOUR_DIGIT_YEAR_PATTERN.matcher(y)).find())
results.add(matcher.group(1));
});
return results;
}
use of org.marc4j.marc.DataField in project RecordManager2 by moravianlibrary.
the class MarcRecordFactory method recordFactory.
/*
* Leader:
* 3 character tag 000
* space
* 24 character tag's value
* example: "000 00759cam a2200229 a 4500"
*
* ControlField:
* 3 character tag 00[1-9]
* space
* tag's value
* example: "001 001445152"
*
* DataField:
* 3 character tag - all digits
* space
* indicator1
* indicator2
* tag's value, perhaps with internal Subfields: 1 character $
* 1 character tag
* value
* example: "$aCambridge"
* example: "991 00$b201503$cNOV$gg$en"
*
* or
*
* 3 character tag - all digits
* space
* tag's value, perhaps with internal Subfields
* example - indicators set on ' ': "991 $b201503$cNOV$gg$en"
*/
public static MarcRecordImpl recordFactory(List<String> data) throws Exception {
MarcFactory marcFactory = MarcFactoryImpl.newInstance();
Record record = marcFactory.newRecord();
record.addVariableField(marcFactory.newControlField("001", "0"));
for (String field : data) {
String key;
String value;
if (field.length() >= 3) {
key = field.substring(0, 3);
value = (field.length() > 3) ? field.substring(4, field.length()) : "";
} else
continue;
// Leader
if (Pattern.matches("000", key)) {
if (value.length() > 24)
value = value.substring(0, 24);
else
value = String.format("%-24s", value);
record.setLeader(marcFactory.newLeader(value));
} else // ControlField
if (Pattern.matches("00[1-9]", key))
record.addVariableField(marcFactory.newControlField(key, value));
else // DataField
if (Pattern.matches("\\w{3}", key)) {
if (value.isEmpty())
record.addVariableField(marcFactory.newDataField(key, ' ', ' '));
else if (value.length() >= 2) {
// Indicators
DataField dataField;
if (value.charAt(0) == '$')
dataField = marcFactory.newDataField(key, ' ', ' ');
else if (value.charAt(1) == '$')
dataField = marcFactory.newDataField(key, value.charAt(0), ' ');
else
dataField = marcFactory.newDataField(key, value.charAt(0), value.charAt(1));
// Subfield
if (value.length() > 2) {
Pattern pattern = Pattern.compile("\\$([a-zA-Z0-9])([^$]*)(.*)");
Matcher matcher = pattern.matcher(value);
while (matcher.find()) {
dataField.addSubfield(marcFactory.newSubfield(matcher.group(1).charAt(0), matcher.group(2)));
matcher = pattern.matcher(matcher.group(3));
}
}
record.addVariableField(dataField);
}
}
}
return new MarcRecordImpl(record);
}
use of org.marc4j.marc.DataField in project RecordManager2 by moravianlibrary.
the class KkvyMarcInterceptor method intercept.
@Override
public byte[] intercept() {
if (super.getRecord() == null) {
return new byte[0];
}
MarcRecord marc = new MarcRecordImpl(super.getRecord());
Record newRecord = new RecordImpl();
newRecord.setLeader(getRecord().getLeader());
for (ControlField cf : super.getRecord().getControlFields()) {
newRecord.addVariableField(cf);
}
Map<String, List<DataField>> dfMap = marc.getAllFields();
for (String tag : new TreeSet<String>(dfMap.keySet())) {
// sorted tags
for (DataField df : dfMap.get(tag)) {
// kill fields 996l = VF
if (df.getTag().equals("996")) {
if (df.getSubfield('l') != null && df.getSubfield('l').getData().trim().equals("VF"))
continue;
}
processField996(df);
newRecord.addVariableField(df);
}
}
return new MarcRecordImpl(newRecord).export(IOFormat.XML_MARC).getBytes(StandardCharsets.UTF_8);
}
Aggregations