![JAR search and dependency download from the Maven repository](/logo.png)
info.freelibrary.marc4j.impl.RecordImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of freelib-marc4j Show documentation
Show all versions of freelib-marc4j Show documentation
An easy to use Application Programming Interface (API) for working with MARC and MARCXML in
Java.
/**
* Copyright (C) 2004 Bas Peters
*
* This file is part of MARC4J
*
* MARC4J is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* MARC4J is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with MARC4J; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package info.freelibrary.marc4j.impl;
import java.util.Collections;
import org.marc4j.marc.VariableField;
import org.marc4j.marc.DataField;
import org.marc4j.marc.ControlField;
import org.marc4j.marc.Leader;
import org.marc4j.marc.Record;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Represents a MARC record.
*
* @author Bas Peters
*/
public class RecordImpl implements Record {
private Long id;
private Leader leader;
protected List controlFields;
protected List dataFields;
private String type;
/**
* Creates a new Record
.
*/
public RecordImpl() {
controlFields = new ArrayList();
dataFields = new ArrayList();
}
/**
* Sets the type of this {@link Record}.
*
* @param type A {@link Record} type
*/
public void setType(String type) {
this.type = type;
}
/**
* Gets the type of this {@link Record}.
*
* @return This {@link Record}'s type
*/
public String getType() {
return type;
}
/**
* Sets this {@link Record}'s {@link Leader}.
*
* @param leader A {@link Leader} to use in this record
*/
public void setLeader(Leader leader) {
this.leader = leader;
}
/**
* Gets the {@link Leader} for this {@link Record}.
*
* @return The {@link Leader} for this {@link Record}
*/
public Leader getLeader() {
return leader;
}
/**
* Adds a VariableField
being a ControlField
or
* DataField
.
*
* If the VariableField
is a control number field (001) and the
* record already has a control number field, the field is replaced with the
* new instance.
*
* @param field the VariableField
*/
public void addVariableField(VariableField field) {
if (field instanceof ControlField) {
ControlField controlField = (ControlField) field;
if (field.getTag().equals("001")) {
if (getControlNumberField() == null) {
controlFields.add(0, controlField);
} else {
controlFields.set(0, controlField);
}
} else {
controlFields.add(controlField);
}
} else {
dataFields.add((DataField) field);
}
}
/**
* Removes the supplied {@link VariableField}
*/
public void removeVariableField(VariableField field) {
if (field instanceof ControlField) {
controlFields.remove(field);
} else {
dataFields.remove(field);
}
}
/**
* Returns the control number field or null
if no control
* number field is available.
*
* @return ControlField - the control number field
*/
public ControlField getControlNumberField() {
for (int index = 0; index < controlFields.size(); index++) {
ControlField field = controlFields.get(index);
if (field.getTag().equals("001")) {
return field;
}
}
return null;
}
/**
* Gets a {@link List} of {@link ControlField}s from the {@link Record}.
*/
public List getControlFields() {
return controlFields;
}
/**
* Gets a {@link List} of {@link DataField}s from the {@link Record}.
*/
public List getDataFields() {
return dataFields;
}
/**
* Gets the first {@link VariableField} with the supplied tag.
*
* @param aTag The tag of the field to be returned
*/
public VariableField getVariableField(String aTag) {
Iterator extends VariableField> iterator = getIterator(aTag);
while (iterator.hasNext()) {
VariableField field = iterator.next();
if (field.getTag().equals(aTag)) {
return field;
}
}
return null;
}
/**
* Gets a {@link List} of {@link VariableField}s with the supplied tag.
*/
public List getVariableFields(String aTag) {
List fields = new ArrayList();
Iterator extends VariableField> iterator = getIterator(aTag);
while (iterator.hasNext()) {
VariableField field = iterator.next();
if (field.getTag().equals(aTag)) {
fields.add(field);
}
}
return fields;
}
/**
* Gets a {@link List} of {@link VariableField}s from the {@link Record}.
*/
public List getVariableFields() {
List fields = new ArrayList();
Iterator extends VariableField> iterator = controlFields.iterator();
while (iterator.hasNext()) {
fields.add(iterator.next());
}
iterator = dataFields.iterator();
while (iterator.hasNext()) {
fields.add(iterator.next());
}
return fields;
}
/**
* Gets the {@link Record}'s control number.
*/
public String getControlNumber() {
ControlField f = getControlNumberField();
if (f == null || f.getData() == null) {
return null;
} else {
return f.getData();
}
}
/**
* Gets the {@link VariableField}s in the {@link Record} with the supplied
* tags.
*/
public List getVariableFields(String[] tags) {
List list = new ArrayList();
for (int i = 0; i < tags.length; i++) {
String tag = tags[i];
List fields = getVariableFields(tag);
if (fields.size() > 0) {
list.addAll(fields);
}
}
return list;
}
/**
* Returns a string representation of this record.
*
*
* Example:
*
*
*
*
* LEADER 00714cam a2200205 a 4500
* 001 12883376
* 005 20030616111422.0
* 008 020805s2002 nyu j 000 1 eng
* 020 $a0786808772
* 020 $a0786816155 (pbk.)
* 040 $aDLC$cDLC$dDLC
* 100 1 $aChabon, Michael.
* 245 10$aSummerland /$cMichael Chabon.
* 250 $a1st ed.
* 260 $aNew York :$bMiramax Books/Hyperion Books for Children,$cc2002.
* 300 $a500 p. ;$c22 cm.
* 650 1$aFantasy.
* 650 1$aBaseball$vFiction.
* 650 1$aMagic$vFiction.
*
*
*
* @return String - a string representation of this record
*/
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("LEADER ");
sb.append(getLeader().toString());
sb.append('\n');
for (VariableField field : getVariableFields()) {
sb.append(field.toString());
sb.append('\n');
}
return sb.toString();
}
/**
* Finds all the {@link VariableField}s that match the supplied regular
* expression pattern.
*/
public List find(String pattern) {
List result = new ArrayList();
Iterator extends VariableField> i = controlFields.iterator();
while (i.hasNext()) {
VariableField field = (VariableField) i.next();
if (field.find(pattern)) {
result.add(field);
}
}
i = dataFields.iterator();
while (i.hasNext()) {
VariableField field = (VariableField) i.next();
if (field.find(pattern)) {
result.add(field);
}
}
return result;
}
/**
* Finds all the {@link VariableField}s that match the supplied tag and
* regular expression pattern.
*/
public List find(String tag, String pattern) {
List result = new ArrayList();
for (VariableField field : getVariableFields(tag)) {
if (field.find(pattern)) {
result.add(field);
}
}
return result;
}
/**
* Finds all the {@link VariableField}s that match the supplied tags and
* regular expression pattern.
*/
public List find(String[] tag, String pattern) {
List result = new ArrayList();
for (VariableField field : getVariableFields(tag)) {
if (field.find(pattern)) {
result.add(field);
}
}
return result;
}
/**
* Sets the ID for this {@link Record}.
*
* @param id The ID for this {@link Record}
*/
public void setId(Long id) {
this.id = id;
}
/**
* Returns the ID for this {@link Record}.
*
* @return An ID for this {@link Record}
*/
public Long getId() {
return id;
}
private Iterator extends VariableField> getIterator(String aTag) {
int tag;
if (aTag.length() == 3) {
try {
tag = Integer.parseInt(aTag);
if (tag > 0 && tag < 10) {
return controlFields.iterator();
} else if (tag >= 10 && tag <= 999) {
return dataFields.iterator();
}
} catch (NumberFormatException details) {
// Log warning below...
}
}
// TODO: log a warning here
return Collections. emptyList().iterator();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy