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
38 /** The logical record length (Position 0-4). */
\r
39 private int recordLength;
\r
41 /** The record status (Position 5). */
\r
42 private char recordStatus;
\r
44 /** Type of record (Position 6). */
\r
45 private char typeOfRecord;
\r
47 /** Implementation defined (Position 7-8). */
\r
48 private char[] implDefined1;
\r
50 /** Character coding scheme (Position 9). */
\r
51 private char charCodingScheme;
\r
53 /** The indicator count (Position 10). */
\r
54 private int indicatorCount;
\r
56 /** The subfield code length (Position 11). */
\r
57 private int subfieldCodeLength;
\r
59 /** The base address of data (Position 12-16). */
\r
60 private int baseAddressOfData;
\r
62 /** Implementation defined (Position 17-18) */
\r
63 private char[] implDefined2;
\r
65 /** Entry map (Position 19-23). */
\r
66 private char[] entryMap;
\r
68 /** number format for both record length and base address of data */
\r
69 DecimalFormat df = new DecimalFormat("00000");
\r
72 * Default constructor.
\r
74 public LeaderImpl() {
\r
78 * Creates a new leader from a String object.
\r
81 * the leader string value
\r
83 public LeaderImpl(String ldr) {
\r
88 * Sets the logical record length (positions 00-04).
\r
90 * @param recordLength
\r
91 * integer representing the record length
\r
93 public void setRecordLength(int recordLength) {
\r
94 this.recordLength = recordLength;
\r
98 * Sets the record status (position 05).
\r
100 * @param recordStatus
\r
101 * character representing the record status
\r
103 public void setRecordStatus(char recordStatus) {
\r
104 this.recordStatus = recordStatus;
\r
108 * Sets the type of record (position 06).
\r
110 * @param typeOfRecord
\r
111 * character representing the type of record
\r
113 public void setTypeOfRecord(char typeOfRecord) {
\r
114 this.typeOfRecord = typeOfRecord;
\r
118 * Sets implementation defined values (position 07-08).
\r
120 * @param implDefined1
\r
121 * character array representing the implementation defined data
\r
123 public void setImplDefined1(char[] implDefined1) {
\r
124 this.implDefined1 = implDefined1;
\r
128 * Sets the character encoding scheme (position 09).
\r
130 * @param charCodingScheme
\r
131 * character representing the character encoding
\r
133 public void setCharCodingScheme(char charCodingScheme) {
\r
134 this.charCodingScheme = charCodingScheme;
\r
138 * Sets the indicator count (position 10).
\r
140 * @param indicatorCount
\r
141 * integer representing the number of indicators present in a
\r
144 public void setIndicatorCount(int indicatorCount) {
\r
145 this.indicatorCount = indicatorCount;
\r
149 * Sets the subfield code length (position 11).
\r
151 * @param subfieldCodeLength
\r
152 * integer representing the subfield code length
\r
154 public void setSubfieldCodeLength(int subfieldCodeLength) {
\r
155 this.subfieldCodeLength = subfieldCodeLength;
\r
159 * Sets the base address of data (positions 12-16).
\r
161 * @param baseAddressOfData
\r
162 * integer representing the base address of data
\r
164 public void setBaseAddressOfData(int baseAddressOfData) {
\r
165 this.baseAddressOfData = baseAddressOfData;
\r
169 * Sets implementation defined values (positions 17-19).
\r
171 * @param implDefined2
\r
172 * character array representing the implementation defined data
\r
174 public void setImplDefined2(char[] implDefined2) {
\r
175 this.implDefined2 = implDefined2;
\r
179 * Sets the entry map (positions 20-23).
\r
182 * character array representing the entry map
\r
184 public void setEntryMap(char[] entryMap) {
\r
185 this.entryMap = entryMap;
\r
189 * Returns the logical record length (positions 00-04).
\r
191 * @return <code>int</code>- the record length
\r
193 public int getRecordLength() {
\r
194 return recordLength;
\r
198 * Returns the record status (positions 05).
\r
200 * @return <code>char</code>- the record status
\r
202 public char getRecordStatus() {
\r
203 return recordStatus;
\r
207 * Returns the record type (position 06).
\r
209 * @return <code>char</code>- the record type
\r
211 public char getTypeOfRecord() {
\r
212 return typeOfRecord;
\r
216 * Returns implementation defined values (positions 07-08).
\r
218 * @return <code>char[]</code>- implementation defined values
\r
220 public char[] getImplDefined1() {
\r
221 return implDefined1;
\r
225 * Returns the character coding scheme (position 09).
\r
227 * @return <code>char</code>- the character coding scheme
\r
229 public char getCharCodingScheme() {
\r
230 return charCodingScheme;
\r
234 * Returns the indicator count (positions 10).
\r
236 * @return <code>int</code>- the indicator count
\r
238 public int getIndicatorCount() {
\r
239 return indicatorCount;
\r
243 * Returns the subfield code length (position 11).
\r
245 * @return <code>int</code>- the subfield code length
\r
247 public int getSubfieldCodeLength() {
\r
248 return subfieldCodeLength;
\r
252 * Returns the base address of data (positions 12-16).
\r
254 * @return <code>int</code>- the base address of data
\r
256 public int getBaseAddressOfData() {
\r
257 return baseAddressOfData;
\r
261 * Returns implementation defined values (positions 17-19).
\r
263 * @return <code>char[]</code>- implementation defined values
\r
265 public char[] getImplDefined2() {
\r
266 return implDefined2;
\r
270 * Returns the entry map (positions 20-23).
\r
272 * @return <code>char[]</code>- the entry map
\r
274 public char[] getEntryMap() {
\r
280 * Creates a leader object from a string object.
\r
284 * Indicator count and subfield code length are defaulted to 2 if they are
\r
285 * not integer values.
\r
291 public void unmarshal(String ldr) {
\r
294 s = ldr.substring(0, 5);
\r
296 setRecordLength(Integer.parseInt(s));
\r
298 setRecordLength(0);
\r
299 setRecordStatus(ldr.charAt(5));
\r
300 setTypeOfRecord(ldr.charAt(6));
\r
301 setImplDefined1(ldr.substring(7, 9).toCharArray());
\r
302 setCharCodingScheme(ldr.charAt(9));
\r
303 s = String.valueOf(ldr.charAt(10));
\r
305 setIndicatorCount(Integer.parseInt(s));
\r
307 setIndicatorCount(2);
\r
308 s = String.valueOf(ldr.charAt(10));
\r
310 setSubfieldCodeLength(Integer.parseInt(s));
\r
312 setSubfieldCodeLength(2);
\r
313 s = ldr.substring(12, 17);
\r
315 setBaseAddressOfData(Integer.parseInt(s));
\r
317 setBaseAddressOfData(0);
\r
318 setImplDefined2(ldr.substring(17, 20).toCharArray());
\r
319 setEntryMap(ldr.substring(20, 24).toCharArray());
\r
320 } catch (NumberFormatException e) {
\r
321 throw new RuntimeException("Unable to parse leader", e);
\r
326 * Creates a string object from this leader object.
\r
328 * @return String - the string object from this leader object
\r
330 public String marshal() {
\r
331 return this.toString();
\r
335 * Returns a string representation of this leader.
\r
341 * 00714cam a2200205 a 4500
\r
344 public String toString() {
\r
345 return new StringBuffer().append(format5.format(getRecordLength()))
\r
346 .append(getRecordStatus()).append(getTypeOfRecord()).append(
\r
347 getImplDefined1()).append(getCharCodingScheme())
\r
348 .append(getIndicatorCount()).append(getSubfieldCodeLength())
\r
349 .append(format5.format(getBaseAddressOfData())).append(
\r
350 getImplDefined2()).append(getEntryMap()).toString();
\r
353 private boolean isInteger(String value) {
\r
354 int len = value.length();
\r
359 switch (value.charAt(i)) {
\r
374 } while (++i < len);
\r
378 private static DecimalFormat format5 = new DecimalFormat("00000");
\r
380 public void setId(Long id) {
\r
384 public Long getId() {
\r