1 // $Id: LeaderImpl.java,v 1.3 2006/08/04 12:32:37 bpeters Exp $
\r
3 * Copyright (C) 2004 Bas Peters
\r
5 * This file is part of MARC4J
\r
7 * MARC4J is free software; you can redistribute it and/or
\r
8 * modify it under the terms of the GNU Lesser General Public
\r
9 * License as published by the Free Software Foundation; either
\r
10 * version 2.1 of the License, or (at your option) any later version.
\r
12 * MARC4J is distributed in the hope that it will be useful,
\r
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
15 * Lesser General Public License for more details.
\r
17 * You should have received a copy of the GNU Lesser General Public
\r
18 * License along with MARC4J; if not, write to the Free Software
\r
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
21 package org.marc4j.marc.impl;
\r
23 import java.text.DecimalFormat;
\r
25 import org.marc4j.marc.Leader;
\r
28 * Represents a record label in a MARC record.
\r
30 * @author Bas Peters
\r
31 * @version $Revision: 1.3 $
\r
34 public class LeaderImpl implements Leader {
\r
39 private static final long serialVersionUID = 6483942612907658766L;
\r
43 /** The logical record length (Position 0-4). */
\r
44 private int recordLength;
\r
46 /** The record status (Position 5). */
\r
47 private char recordStatus;
\r
49 /** Type of record (Position 6). */
\r
50 private char typeOfRecord;
\r
52 /** Implementation defined (Position 7-8). */
\r
53 private char[] implDefined1;
\r
55 /** Character coding scheme (Position 9). */
\r
56 private char charCodingScheme;
\r
58 /** The indicator count (Position 10). */
\r
59 private int indicatorCount;
\r
61 /** The subfield code length (Position 11). */
\r
62 private int subfieldCodeLength;
\r
64 /** The base address of data (Position 12-16). */
\r
65 private int baseAddressOfData;
\r
67 /** Implementation defined (Position 17-18) */
\r
68 private char[] implDefined2;
\r
70 /** Entry map (Position 19-23). */
\r
71 private char[] entryMap;
\r
73 /** number format for both record length and base address of data */
\r
74 DecimalFormat df = new DecimalFormat("00000");
\r
77 * Default constructor.
\r
79 public LeaderImpl() {
\r
83 * Creates a new leader from a String object.
\r
86 * the leader string value
\r
88 public LeaderImpl(String ldr) {
\r
93 * Sets the logical record length (positions 00-04).
\r
95 * @param recordLength
\r
96 * integer representing the record length
\r
98 public void setRecordLength(int recordLength) {
\r
99 this.recordLength = recordLength;
\r
103 * Sets the record status (position 05).
\r
105 * @param recordStatus
\r
106 * character representing the record status
\r
108 public void setRecordStatus(char recordStatus) {
\r
109 this.recordStatus = recordStatus;
\r
113 * Sets the type of record (position 06).
\r
115 * @param typeOfRecord
\r
116 * character representing the type of record
\r
118 public void setTypeOfRecord(char typeOfRecord) {
\r
119 this.typeOfRecord = typeOfRecord;
\r
123 * Sets implementation defined values (position 07-08).
\r
125 * @param implDefined1
\r
126 * character array representing the implementation defined data
\r
128 public void setImplDefined1(char[] implDefined1) {
\r
129 this.implDefined1 = implDefined1;
\r
133 * Sets the character encoding scheme (position 09).
\r
135 * @param charCodingScheme
\r
136 * character representing the character encoding
\r
138 public void setCharCodingScheme(char charCodingScheme) {
\r
139 this.charCodingScheme = charCodingScheme;
\r
143 * Sets the indicator count (position 10).
\r
145 * @param indicatorCount
\r
146 * integer representing the number of indicators present in a
\r
149 public void setIndicatorCount(int indicatorCount) {
\r
150 this.indicatorCount = indicatorCount;
\r
154 * Sets the subfield code length (position 11).
\r
156 * @param subfieldCodeLength
\r
157 * integer representing the subfield code length
\r
159 public void setSubfieldCodeLength(int subfieldCodeLength) {
\r
160 this.subfieldCodeLength = subfieldCodeLength;
\r
164 * Sets the base address of data (positions 12-16).
\r
166 * @param baseAddressOfData
\r
167 * integer representing the base address of data
\r
169 public void setBaseAddressOfData(int baseAddressOfData) {
\r
170 this.baseAddressOfData = baseAddressOfData;
\r
174 * Sets implementation defined values (positions 17-19).
\r
176 * @param implDefined2
\r
177 * character array representing the implementation defined data
\r
179 public void setImplDefined2(char[] implDefined2) {
\r
180 this.implDefined2 = implDefined2;
\r
184 * Sets the entry map (positions 20-23).
\r
187 * character array representing the entry map
\r
189 public void setEntryMap(char[] entryMap) {
\r
190 this.entryMap = entryMap;
\r
194 * Returns the logical record length (positions 00-04).
\r
196 * @return <code>int</code>- the record length
\r
198 public int getRecordLength() {
\r
199 return recordLength;
\r
203 * Returns the record status (positions 05).
\r
205 * @return <code>char</code>- the record status
\r
207 public char getRecordStatus() {
\r
208 return recordStatus;
\r
212 * Returns the record type (position 06).
\r
214 * @return <code>char</code>- the record type
\r
216 public char getTypeOfRecord() {
\r
217 return typeOfRecord;
\r
221 * Returns implementation defined values (positions 07-08).
\r
223 * @return <code>char[]</code>- implementation defined values
\r
225 public char[] getImplDefined1() {
\r
226 return implDefined1;
\r
230 * Returns the character coding scheme (position 09).
\r
232 * @return <code>char</code>- the character coding scheme
\r
234 public char getCharCodingScheme() {
\r
235 return charCodingScheme;
\r
239 * Returns the indicator count (positions 10).
\r
241 * @return <code>int</code>- the indicator count
\r
243 public int getIndicatorCount() {
\r
244 return indicatorCount;
\r
248 * Returns the subfield code length (position 11).
\r
250 * @return <code>int</code>- the subfield code length
\r
252 public int getSubfieldCodeLength() {
\r
253 return subfieldCodeLength;
\r
257 * Returns the base address of data (positions 12-16).
\r
259 * @return <code>int</code>- the base address of data
\r
261 public int getBaseAddressOfData() {
\r
262 return baseAddressOfData;
\r
266 * Returns implementation defined values (positions 17-19).
\r
268 * @return <code>char[]</code>- implementation defined values
\r
270 public char[] getImplDefined2() {
\r
271 return implDefined2;
\r
275 * Returns the entry map (positions 20-23).
\r
277 * @return <code>char[]</code>- the entry map
\r
279 public char[] getEntryMap() {
\r
285 * Creates a leader object from a string object.
\r
289 * Indicator count and subfield code length are defaulted to 2 if they are
\r
290 * not integer values.
\r
296 public void unmarshal(String ldr) {
\r
299 s = ldr.substring(0, 5);
\r
301 setRecordLength(Integer.parseInt(s));
\r
303 setRecordLength(0);
\r
304 setRecordStatus(ldr.charAt(5));
\r
305 setTypeOfRecord(ldr.charAt(6));
\r
306 setImplDefined1(ldr.substring(7, 9).toCharArray());
\r
307 setCharCodingScheme(ldr.charAt(9));
\r
308 s = String.valueOf(ldr.charAt(10));
\r
310 setIndicatorCount(Integer.parseInt(s));
\r
312 setIndicatorCount(2);
\r
313 s = String.valueOf(ldr.charAt(10));
\r
315 setSubfieldCodeLength(Integer.parseInt(s));
\r
317 setSubfieldCodeLength(2);
\r
318 s = ldr.substring(12, 17);
\r
320 setBaseAddressOfData(Integer.parseInt(s));
\r
322 setBaseAddressOfData(0);
\r
323 setImplDefined2(ldr.substring(17, 20).toCharArray());
\r
324 setEntryMap(ldr.substring(20, 24).toCharArray());
\r
325 } catch (NumberFormatException e) {
\r
326 throw new RuntimeException("Unable to parse leader", e);
\r
331 * Creates a string object from this leader object.
\r
333 * @return String - the string object from this leader object
\r
335 public String marshal() {
\r
336 return this.toString();
\r
340 * Returns a string representation of this leader.
\r
346 * 00714cam a2200205 a 4500
\r
349 public String toString() {
\r
350 return new StringBuffer().append(format5.format(getRecordLength()))
\r
351 .append(getRecordStatus()).append(getTypeOfRecord()).append(
\r
352 getImplDefined1()).append(getCharCodingScheme())
\r
353 .append(getIndicatorCount()).append(getSubfieldCodeLength())
\r
354 .append(format5.format(getBaseAddressOfData())).append(
\r
355 getImplDefined2()).append(getEntryMap()).toString();
\r
358 private boolean isInteger(String value) {
\r
359 int len = value.length();
\r
364 switch (value.charAt(i)) {
\r
379 } while (++i < len);
\r
383 private static DecimalFormat format5 = new DecimalFormat("00000");
\r
385 public void setId(Long id) {
\r
389 public Long getId() {
\r