Don't throw IllegalArgumentException in RSS reader; use checked FormatException to fit into BS error checking mechanism correctly

git-svn-id: https://zxing.googlecode.com/svn/trunk@2893 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen@gmail.com 2013-09-13 12:01:50 +00:00
parent 6c103e7840
commit f84c8470a4
9 changed files with 31 additions and 29 deletions

View file

@ -28,6 +28,7 @@ package com.google.zxing.oned.rss.expanded;
import com.google.zxing.BarcodeFormat; import com.google.zxing.BarcodeFormat;
import com.google.zxing.DecodeHintType; import com.google.zxing.DecodeHintType;
import com.google.zxing.FormatException;
import com.google.zxing.NotFoundException; import com.google.zxing.NotFoundException;
import com.google.zxing.Result; import com.google.zxing.Result;
import com.google.zxing.ResultPoint; import com.google.zxing.ResultPoint;
@ -122,7 +123,7 @@ public final class RSSExpandedReader extends AbstractRSSReader {
@Override @Override
public Result decodeRow(int rowNumber, public Result decodeRow(int rowNumber,
BitArray row, BitArray row,
Map<DecodeHintType,?> hints) throws NotFoundException { Map<DecodeHintType,?> hints) throws NotFoundException, FormatException {
// Rows can start with even pattern in case in prev rows there where odd number of patters. // Rows can start with even pattern in case in prev rows there where odd number of patters.
// So lets try twice // So lets try twice
this.pairs.clear(); this.pairs.clear();
@ -362,7 +363,7 @@ public final class RSSExpandedReader extends AbstractRSSReader {
} }
// Not private for unit testing // Not private for unit testing
static Result constructResult(List<ExpandedPair> pairs) throws NotFoundException{ static Result constructResult(List<ExpandedPair> pairs) throws NotFoundException, FormatException {
BitArray binary = BitArrayBuilder.buildBitArray(pairs); BitArray binary = BitArrayBuilder.buildBitArray(pairs);
AbstractExpandedDecoder decoder = AbstractExpandedDecoder.createDecoder(binary); AbstractExpandedDecoder decoder = AbstractExpandedDecoder.createDecoder(binary);

View file

@ -26,6 +26,7 @@
package com.google.zxing.oned.rss.expanded.decoders; package com.google.zxing.oned.rss.expanded.decoders;
import com.google.zxing.FormatException;
import com.google.zxing.NotFoundException; import com.google.zxing.NotFoundException;
import com.google.zxing.common.BitArray; import com.google.zxing.common.BitArray;
@ -42,7 +43,7 @@ final class AI01392xDecoder extends AI01decoder {
} }
@Override @Override
public String parseInformation() throws NotFoundException { public String parseInformation() throws NotFoundException, FormatException {
if (this.getInformation().getSize() < HEADER_SIZE + GTIN_SIZE) { if (this.getInformation().getSize() < HEADER_SIZE + GTIN_SIZE) {
throw NotFoundException.getNotFoundInstance(); throw NotFoundException.getNotFoundInstance();
} }

View file

@ -25,6 +25,7 @@
*/ */
package com.google.zxing.oned.rss.expanded.decoders; package com.google.zxing.oned.rss.expanded.decoders;
import com.google.zxing.FormatException;
import com.google.zxing.NotFoundException; import com.google.zxing.NotFoundException;
import com.google.zxing.common.BitArray; import com.google.zxing.common.BitArray;
@ -42,7 +43,7 @@ final class AI01393xDecoder extends AI01decoder {
} }
@Override @Override
public String parseInformation() throws NotFoundException { public String parseInformation() throws NotFoundException, FormatException {
if(this.getInformation().getSize() < HEADER_SIZE + GTIN_SIZE) { if(this.getInformation().getSize() < HEADER_SIZE + GTIN_SIZE) {
throw NotFoundException.getNotFoundInstance(); throw NotFoundException.getNotFoundInstance();
} }

View file

@ -26,6 +26,7 @@
package com.google.zxing.oned.rss.expanded.decoders; package com.google.zxing.oned.rss.expanded.decoders;
import com.google.zxing.FormatException;
import com.google.zxing.NotFoundException; import com.google.zxing.NotFoundException;
import com.google.zxing.common.BitArray; import com.google.zxing.common.BitArray;
@ -42,7 +43,7 @@ final class AI01AndOtherAIs extends AI01decoder {
} }
@Override @Override
public String parseInformation() throws NotFoundException { public String parseInformation() throws NotFoundException, FormatException {
StringBuilder buff = new StringBuilder(); StringBuilder buff = new StringBuilder();
buff.append("(01)"); buff.append("(01)");

View file

@ -26,6 +26,7 @@
package com.google.zxing.oned.rss.expanded.decoders; package com.google.zxing.oned.rss.expanded.decoders;
import com.google.zxing.FormatException;
import com.google.zxing.NotFoundException; import com.google.zxing.NotFoundException;
import com.google.zxing.common.BitArray; import com.google.zxing.common.BitArray;
@ -51,7 +52,7 @@ public abstract class AbstractExpandedDecoder {
return generalDecoder; return generalDecoder;
} }
public abstract String parseInformation() throws NotFoundException; public abstract String parseInformation() throws NotFoundException, FormatException;
public static AbstractExpandedDecoder createDecoder(BitArray information){ public static AbstractExpandedDecoder createDecoder(BitArray information){
if (information.get(1)) { if (information.get(1)) {

View file

@ -26,6 +26,7 @@
package com.google.zxing.oned.rss.expanded.decoders; package com.google.zxing.oned.rss.expanded.decoders;
import com.google.zxing.FormatException;
import com.google.zxing.NotFoundException; import com.google.zxing.NotFoundException;
import com.google.zxing.common.BitArray; import com.google.zxing.common.BitArray;
@ -42,7 +43,7 @@ final class AnyAIDecoder extends AbstractExpandedDecoder {
} }
@Override @Override
public String parseInformation() throws NotFoundException { public String parseInformation() throws NotFoundException, FormatException {
StringBuilder buf = new StringBuilder(); StringBuilder buf = new StringBuilder();
return this.getGeneralDecoder().decodeAllCodes(buf, HEADER_SIZE); return this.getGeneralDecoder().decodeAllCodes(buf, HEADER_SIZE);
} }

View file

@ -26,6 +26,8 @@
package com.google.zxing.oned.rss.expanded.decoders; package com.google.zxing.oned.rss.expanded.decoders;
import com.google.zxing.FormatException;
/** /**
* @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es) * @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es)
* @author Eduardo Castillejo, University of Deusto (eduardo.castillejo@deusto.es) * @author Eduardo Castillejo, University of Deusto (eduardo.castillejo@deusto.es)
@ -37,19 +39,15 @@ final class DecodedNumeric extends DecodedObject {
static final int FNC1 = 10; static final int FNC1 = 10;
DecodedNumeric(int newPosition, int firstDigit, int secondDigit){ DecodedNumeric(int newPosition, int firstDigit, int secondDigit) throws FormatException {
super(newPosition); super(newPosition);
if (firstDigit < 0 || firstDigit > 10 || secondDigit < 0 || secondDigit > 10) {
throw FormatException.getFormatInstance();
}
this.firstDigit = firstDigit; this.firstDigit = firstDigit;
this.secondDigit = secondDigit; this.secondDigit = secondDigit;
if (this.firstDigit < 0 || this.firstDigit > 10) {
throw new IllegalArgumentException("Invalid firstDigit: " + firstDigit);
}
if (this.secondDigit < 0 || this.secondDigit > 10) {
throw new IllegalArgumentException("Invalid secondDigit: " + secondDigit);
}
} }
int getFirstDigit(){ int getFirstDigit(){

View file

@ -26,6 +26,7 @@
package com.google.zxing.oned.rss.expanded.decoders; package com.google.zxing.oned.rss.expanded.decoders;
import com.google.zxing.FormatException;
import com.google.zxing.NotFoundException; import com.google.zxing.NotFoundException;
import com.google.zxing.common.BitArray; import com.google.zxing.common.BitArray;
@ -43,7 +44,7 @@ final class GeneralAppIdDecoder {
this.information = information; this.information = information;
} }
String decodeAllCodes(StringBuilder buff, int initialPosition) throws NotFoundException { String decodeAllCodes(StringBuilder buff, int initialPosition) throws NotFoundException, FormatException {
int currentPosition = initialPosition; int currentPosition = initialPosition;
String remaining = null; String remaining = null;
do{ do{
@ -83,7 +84,7 @@ final class GeneralAppIdDecoder {
return this.information.get(pos + 3); return this.information.get(pos + 3);
} }
private DecodedNumeric decodeNumeric(int pos) { private DecodedNumeric decodeNumeric(int pos) throws FormatException {
if(pos + 7 > this.information.getSize()){ if(pos + 7 > this.information.getSize()){
int numeric = extractNumericValueFromBitArray(pos, 4); int numeric = extractNumericValueFromBitArray(pos, 4);
if(numeric == 0) { if(numeric == 0) {
@ -104,10 +105,6 @@ final class GeneralAppIdDecoder {
} }
static int extractNumericValueFromBitArray(BitArray information, int pos, int bits) { static int extractNumericValueFromBitArray(BitArray information, int pos, int bits) {
if(bits > 32) {
throw new IllegalArgumentException("extractNumberValueFromBitArray can't handle more than 32 bits");
}
int value = 0; int value = 0;
for (int i = 0; i < bits; ++i) { for (int i = 0; i < bits; ++i) {
if (information.get(pos + i)) { if (information.get(pos + i)) {
@ -118,7 +115,7 @@ final class GeneralAppIdDecoder {
return value; return value;
} }
DecodedInformation decodeGeneralPurposeField(int pos, String remaining) { DecodedInformation decodeGeneralPurposeField(int pos, String remaining) throws FormatException {
this.buffer.setLength(0); this.buffer.setLength(0);
if(remaining != null) { if(remaining != null) {
@ -134,7 +131,7 @@ final class GeneralAppIdDecoder {
return new DecodedInformation(this.current.getPosition(), this.buffer.toString()); return new DecodedInformation(this.current.getPosition(), this.buffer.toString());
} }
private DecodedInformation parseBlocks() { private DecodedInformation parseBlocks() throws FormatException {
boolean isFinished; boolean isFinished;
BlockParsedResult result; BlockParsedResult result;
do{ do{
@ -160,7 +157,7 @@ final class GeneralAppIdDecoder {
return result.getDecodedInformation(); return result.getDecodedInformation();
} }
private BlockParsedResult parseNumericBlock() { private BlockParsedResult parseNumericBlock() throws FormatException {
while (isStillNumeric(current.getPosition())) { while (isStillNumeric(current.getPosition())) {
DecodedNumeric numeric = decodeNumeric(current.getPosition()); DecodedNumeric numeric = decodeNumeric(current.getPosition());
current.setPosition(numeric.getNewPosition()); current.setPosition(numeric.getNewPosition());
@ -190,7 +187,7 @@ final class GeneralAppIdDecoder {
return new BlockParsedResult(false); return new BlockParsedResult(false);
} }
private BlockParsedResult parseIsoIec646Block() { private BlockParsedResult parseIsoIec646Block() throws FormatException {
while (isStillIsoIec646(current.getPosition())) { while (isStillIsoIec646(current.getPosition())) {
DecodedChar iso = decodeIsoIec646(current.getPosition()); DecodedChar iso = decodeIsoIec646(current.getPosition());
current.setPosition(iso.getNewPosition()); current.setPosition(iso.getNewPosition());
@ -273,7 +270,7 @@ final class GeneralAppIdDecoder {
} }
private DecodedChar decodeIsoIec646(int pos) { private DecodedChar decodeIsoIec646(int pos) throws FormatException {
int fiveBitValue = extractNumericValueFromBitArray(pos, 5); int fiveBitValue = extractNumericValueFromBitArray(pos, 5);
if (fiveBitValue == 15) { if (fiveBitValue == 15) {
return new DecodedChar(pos + 5, DecodedChar.FNC1); return new DecodedChar(pos + 5, DecodedChar.FNC1);
@ -360,7 +357,7 @@ final class GeneralAppIdDecoder {
c = ' '; c = ' ';
break; break;
default: default:
throw new IllegalArgumentException("Decoding invalid ISO/IEC 646 value: " + eightBitValue); throw FormatException.getFormatInstance();
} }
return new DecodedChar(pos + 8, c); return new DecodedChar(pos + 8, c);
} }

View file

@ -26,6 +26,7 @@
package com.google.zxing.oned.rss.expanded.decoders; package com.google.zxing.oned.rss.expanded.decoders;
import com.google.zxing.FormatException;
import com.google.zxing.NotFoundException; import com.google.zxing.NotFoundException;
import com.google.zxing.common.BitArray; import com.google.zxing.common.BitArray;
import com.google.zxing.oned.rss.expanded.BinaryUtil; import com.google.zxing.oned.rss.expanded.BinaryUtil;
@ -67,7 +68,7 @@ public abstract class AbstractDecoderTest extends Assert {
protected static final String compressedDate_End = "X..X.XX........."; protected static final String compressedDate_End = "X..X.XX.........";
protected static void assertCorrectBinaryString(CharSequence binaryString, protected static void assertCorrectBinaryString(CharSequence binaryString,
String expectedNumber) throws NotFoundException { String expectedNumber) throws NotFoundException, FormatException {
BitArray binary = BinaryUtil.buildBitArrayFromStringWithoutSpaces(binaryString); BitArray binary = BinaryUtil.buildBitArrayFromStringWithoutSpaces(binaryString);
AbstractExpandedDecoder decoder = AbstractExpandedDecoder.createDecoder(binary); AbstractExpandedDecoder decoder = AbstractExpandedDecoder.createDecoder(binary);
String result = decoder.parseInformation(); String result = decoder.parseInformation();