Search in sources :

Example 51 with DataField

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;
}
Also used : DataField(org.marc4j.marc.DataField) Matcher(java.util.regex.Matcher)

Example 52 with DataField

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;
}
Also used : DataField(org.marc4j.marc.DataField) Subfield(org.marc4j.marc.Subfield)

Example 53 with DataField

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;
}
Also used : DataField(org.marc4j.marc.DataField) Matcher(java.util.regex.Matcher) TreeSet(java.util.TreeSet) MarcRecord(cz.mzk.recordmanager.server.marc.MarcRecord)

Example 54 with DataField

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);
}
Also used : Pattern(java.util.regex.Pattern) DataField(org.marc4j.marc.DataField) Matcher(java.util.regex.Matcher) MarcFactory(org.marc4j.marc.MarcFactory) Record(org.marc4j.marc.Record)

Example 55 with DataField

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);
}
Also used : MarcRecordImpl(cz.mzk.recordmanager.server.marc.MarcRecordImpl) ControlField(org.marc4j.marc.ControlField) DataField(org.marc4j.marc.DataField) TreeSet(java.util.TreeSet) MarcRecord(cz.mzk.recordmanager.server.marc.MarcRecord) Record(org.marc4j.marc.Record) MarcRecord(cz.mzk.recordmanager.server.marc.MarcRecord) List(java.util.List) MarcRecordImpl(cz.mzk.recordmanager.server.marc.MarcRecordImpl) RecordImpl(cz.mzk.recordmanager.server.marc.marc4j.RecordImpl)

Aggregations

DataField (org.marc4j.marc.DataField)82 ArrayList (java.util.ArrayList)18 Subfield (org.marc4j.marc.Subfield)17 MarcRecord (cz.mzk.recordmanager.server.marc.MarcRecord)15 ControlField (org.marc4j.marc.ControlField)13 Record (org.marc4j.marc.Record)13 MarcRecordImpl (cz.mzk.recordmanager.server.marc.MarcRecordImpl)12 RecordImpl (cz.mzk.recordmanager.server.marc.marc4j.RecordImpl)11 List (java.util.List)11 TreeSet (java.util.TreeSet)11 Matcher (java.util.regex.Matcher)11 MarcFactory (org.marc4j.marc.MarcFactory)8 MarcFactoryImpl (cz.mzk.recordmanager.server.marc.marc4j.MarcFactoryImpl)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 NoDataException (cz.mzk.recordmanager.server.util.identifier.NoDataException)4 HarvestedRecord (cz.mzk.recordmanager.server.model.HarvestedRecord)3 IOException (java.io.IOException)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 ImportConfiguration (cz.mzk.recordmanager.server.model.ImportConfiguration)2 ShortTitle (cz.mzk.recordmanager.server.model.ShortTitle)2