|
| 1 | +/* |
| 2 | + * Copyright 2014 Christoph Böhme |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 the "License"; |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.culturegraph.mf.stream.converter.bib; |
| 17 | + |
| 18 | +import org.culturegraph.mf.exceptions.FormatException; |
| 19 | +import org.culturegraph.mf.framework.DefaultStreamPipe; |
| 20 | +import org.culturegraph.mf.framework.ObjectReceiver; |
| 21 | +import org.culturegraph.mf.framework.StreamReceiver; |
| 22 | +import org.culturegraph.mf.framework.annotations.Description; |
| 23 | +import org.culturegraph.mf.framework.annotations.In; |
| 24 | +import org.culturegraph.mf.framework.annotations.Out; |
| 25 | +import org.culturegraph.mf.iso2709.Iso2709Format; |
| 26 | +import org.culturegraph.mf.iso2709.RecordBuilder; |
| 27 | +import org.culturegraph.mf.iso2709.RecordFormat; |
| 28 | + |
| 29 | +/** |
| 30 | + * Encodes a stream in MARC21 format. |
| 31 | + * |
| 32 | + * <p>MARC21 supports two types of fields: reference fields and data fields. |
| 33 | + * Reference fields consist of a name tag and a single value. Data fields have a |
| 34 | + * name tag, two indicators and consist of subfields which have an identifier |
| 35 | + * each. |
| 36 | + * </p> |
| 37 | + * |
| 38 | + * <p>The {@code Marc21Encoder} encodes a stream as follows: |
| 39 | + * </p> |
| 40 | + * |
| 41 | + * <ul> |
| 42 | + * <li>top-level literals are encoded as reference fields. Their name must match |
| 43 | + * the requirements for reference field tags in ISO 2709:2008 records.</li> |
| 44 | + * |
| 45 | + * <li>entities are encoded as data fields. Only one level of entities is |
| 46 | + * supported. The entity name must consist of a three letter tag name followed |
| 47 | + * by two indicator characters. The tag name must follow the requirements for |
| 48 | + * data field tags in ISO 2709:2008 records.</li> |
| 49 | + * |
| 50 | + * <li>Literals in entities are encoded as subfields. The literal name is used |
| 51 | + * as subfield indicator and must therefore be a single character.</li> |
| 52 | + * |
| 53 | + * <li>If a literal named "leader" is encountered it is treated as a ISO |
| 54 | + * 2709:2008 record label and some of its contents (record status, |
| 55 | + * implementation codes, user system characters) are copied into the generated |
| 56 | + * record</li> |
| 57 | + * </ul> |
| 58 | + * |
| 59 | + * <p>The stream expected by the encoder is compatible to the streams emitted by |
| 60 | + * the {@link MarcDecoder} and the {MarcXmlHandler}. |
| 61 | + * </p> |
| 62 | + * |
| 63 | + * <p>The record identifier in {@code startRecord} is ignored. To add an identifier |
| 64 | + * to the MARC21 record a reference field with tag name 001 need to be added. |
| 65 | + * </p> |
| 66 | + * |
| 67 | + * @throws FormatException |
| 68 | + * if the stream cannot be converted into a MARC21 record. |
| 69 | + * |
| 70 | + * @author Christoph Böhme |
| 71 | + * |
| 72 | + */ |
| 73 | +@In(StreamReceiver.class) |
| 74 | +@Out(String.class) |
| 75 | +@Description("Encodes MARC21 records") |
| 76 | +public final class Marc21Encoder extends |
| 77 | + DefaultStreamPipe<ObjectReceiver<String>> { |
| 78 | + |
| 79 | + public static final String LEADER_LITERAL = "leader"; |
| 80 | + |
| 81 | + private static final RecordFormat MARC21 = new RecordFormat(); |
| 82 | + |
| 83 | + private final RecordBuilder builder = new RecordBuilder(MARC21); |
| 84 | + private final int nameLength; |
| 85 | + |
| 86 | + private boolean inField; |
| 87 | + |
| 88 | + // CHECKSTYLE OFF: MagicNumber |
| 89 | + static { |
| 90 | + MARC21.setIndicatorLength(2); |
| 91 | + MARC21.setIdentifierLength(2); |
| 92 | + MARC21.setFieldLengthLength(4); |
| 93 | + MARC21.setFieldStartLength(5); |
| 94 | + MARC21.setImplDefinedPartLength(0); |
| 95 | + } |
| 96 | + // CHECKSTYLE ON: MagicNumber |
| 97 | + |
| 98 | + public Marc21Encoder() { |
| 99 | + super(); |
| 100 | + nameLength = Iso2709Format.TAG_LENGTH + MARC21.getIndicatorLength(); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void startRecord(final String identifier) { |
| 105 | + inField = false; |
| 106 | + builder.reset(); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void endRecord() { |
| 111 | + getReceiver().process(builder.toString()); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public void startEntity(final String name) { |
| 116 | + if (name.length() != nameLength) { |
| 117 | + throw new FormatException("invalid entity name: " + name); |
| 118 | + } |
| 119 | + |
| 120 | + final String tag = name.substring(0, Iso2709Format.TAG_LENGTH); |
| 121 | + final String indicators = name.substring(Iso2709Format.TAG_LENGTH); |
| 122 | + builder.startField(tag, indicators); |
| 123 | + inField = true; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void endEntity() { |
| 128 | + inField = false; |
| 129 | + builder.endField(); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public void literal(final String name, final String value) { |
| 134 | + if (LEADER_LITERAL.equals(name)) { |
| 135 | + setRecordLabel(value); |
| 136 | + } else if (inField) { |
| 137 | + builder.appendSubfield(name, value); |
| 138 | + } else { |
| 139 | + builder.appendReferenceField(name, value); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private void setRecordLabel(final String value) { |
| 144 | + if (value.length() != Iso2709Format.RECORD_LABEL_LENGTH) { |
| 145 | + throw new FormatException("leader must be 24 characters long"); |
| 146 | + } |
| 147 | + builder.setRecordStatus(value.charAt(Iso2709Format.RECORD_STATUS_POS)); |
| 148 | + builder.setImplCodes(value.substring(Iso2709Format.IMPL_CODES_START, |
| 149 | + Iso2709Format.IMPL_CODES_END)); |
| 150 | + builder.setSystemChars(value.substring( |
| 151 | + Iso2709Format.SYSTEM_CHARS_START, |
| 152 | + Iso2709Format.SYSTEM_CHARS_END)); |
| 153 | + } |
| 154 | + |
| 155 | +} |
0 commit comments