From 10af25f58982fc7284f05cb80b12126cf3ee95b7 Mon Sep 17 00:00:00 2001 From: Dennis Schafroth Date: Wed, 4 Jan 2012 20:14:47 +0100 Subject: [PATCH] Possible to skip bad indicators. Replace control characters with space. --- src/org/marc4j/MarcStreamReader.java | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/src/org/marc4j/MarcStreamReader.java b/src/org/marc4j/MarcStreamReader.java index 473450a..9b6f637 100644 --- a/src/org/marc4j/MarcStreamReader.java +++ b/src/org/marc4j/MarcStreamReader.java @@ -83,6 +83,8 @@ public class MarcStreamReader implements MarcReader { private CharConverter converterAnsel = null; + private boolean setBadIndicators = true; + /** * Constructs an instance with the specified input stream. */ @@ -234,7 +236,10 @@ public class MarcStreamReader implements MarcReader { inputrec.readFully(byteArray); try { - record.addVariableField(parseDataField(tags[i], byteArray)); + DataField dataField = parseDataField(tags[i], byteArray); + // dataField could be null if bad indicators + if (dataField != null) + record.addVariableField(dataField); } catch (IOException e) { throw new MarcException( "error parsing data field for tag: " + tags[i] @@ -263,9 +268,16 @@ public class MarcStreamReader implements MarcReader { DataField dataField = factory.newDataField(); dataField.setTag(tag); - dataField.setIndicator1(ind1); - dataField.setIndicator2(ind2); + boolean badIndicatorFound = false; + if (setBadIndicators || ind1 >= ' ' ) + dataField.setIndicator1(ind1); + else + badIndicatorFound = true; + if (setBadIndicators || ind2 >= ' ') + dataField.setIndicator2(ind2); + else + badIndicatorFound = true; int code; int size; int readByte; @@ -294,6 +306,9 @@ public class MarcStreamReader implements MarcReader { break; } } + /* Bad Indicators was found, so dropping field */ + if (badIndicatorFound) + return null; return dataField; } @@ -406,7 +421,12 @@ public class MarcStreamReader implements MarcReader { else if (encoding.equals("MARC-8") || encoding.equals("MARC8")) { if (converterAnsel == null) converterAnsel = new AnselToUnicode(); + + for (int index = 0; index < bytes.length; index++) + if (bytes[index] < 32) + bytes[index] = ' '; dataElement = converterAnsel.convert(bytes); + //dataElement = dataElement.replaceAll("\0", " "); } else if (encoding.equals("ISO-8859-1") || encoding.equals("ISO8859_1")) { @@ -419,5 +439,13 @@ public class MarcStreamReader implements MarcReader { } return dataElement; } + + public boolean isBadIndicators() { + return setBadIndicators; + } + + public void setBadIndicators(boolean trueFalse) { + this.setBadIndicators = trueFalse; + } } \ No newline at end of file -- 1.7.10.4