Skip to content

Commit

Permalink
0002673: DBF Router to provide a parameter to ignore header validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jumpmind-josh committed Jul 8, 2016
1 parent c444e49 commit 3ca2b15
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 50 deletions.
Expand Up @@ -320,6 +320,8 @@ private ParameterConstants() {

public final static String LOG_SQL_PARAMETERS_INLINE = "log.sql.parameters.inline";

public final static String DBF_ROUTER_VALIDATE_HEADER = "dbf.router.validate.header";

public static Map<String, ParameterMetaData> getParameterMetaData() {
return parameterMetaData;
}
Expand Down
Expand Up @@ -7,6 +7,7 @@

import org.jumpmind.extension.IBuiltInExtensionPoint;
import org.jumpmind.symmetric.ISymmetricEngine;
import org.jumpmind.symmetric.common.ParameterConstants;
import org.jumpmind.symmetric.route.parse.DBFReader;

public class DBFRouter extends AbstractFileParsingRouter implements IDataRouter, IBuiltInExtensionPoint {
Expand All @@ -27,7 +28,10 @@ public List<String> parse(File file, int lineNumber) {
List<String> rows = new ArrayList<String>();

try {
dbfReader = new DBFReader(new FileInputStream(file));
boolean validateHeader = engine.getParameterService()
.is(ParameterConstants.DBF_ROUTER_VALIDATE_HEADER, true);

dbfReader = new DBFReader(new FileInputStream(file), validateHeader);
int currentLine = 1;
while (dbfReader.hasNextRecord()) {
StringBuffer row = new StringBuffer();
Expand All @@ -44,7 +48,7 @@ public List<String> parse(File file, int lineNumber) {
}
}
catch (Exception e) {

log.error("Unable to parse DBF file " + file.getName(), e);
}
return rows;
}
Expand All @@ -59,7 +63,7 @@ public String getColumnNames() {
}
}
catch (Exception e) {

log.error("Unable to read column names for DBF file ", e);
}
return columns.toString();
}
Expand Down
Expand Up @@ -9,50 +9,54 @@ public class DBFField {
private char type;
private int length;
private int decimalCount;

public DBFField(String s, char c, int i, int j) throws DBFException {
if (s.length() > 10) {
throw new DBFException("The field name is more than 10 characters long: " + s);
}
if (c != 'C' && c != 'N' && c != 'L' && c != 'D' && c != 'F') {
throw new DBFException("The field type is not a valid. Got: " + c);
}
if (i < 1) {
throw new DBFException("The field length should be a positive integer. Got: " + i);
}
if (c == 'C' && i >= 255) {
throw new DBFException(
"The field length should be less than 255 characters for character fields. Got: " + i);
}
if (c == 'N' && i >= 21) {
throw new DBFException("The field length should be less than 21 digits for numeric fields. Got: " + i);
}
if (c == 'L' && i != 1) {
throw new DBFException("The field length should be 1 characater for logical fields. Got: " + i);
}
if (c == 'D' && i != 8) {
throw new DBFException("The field length should be 8 characaters for date fields. Got: " + i);
}
if (c == 'F' && i >= 21) {
throw new DBFException(
"The field length should be less than 21 digits for floating point fields. Got: " + i);
}
if (j < 0) {
throw new DBFException("The field decimal count should not be a negative integer. Got: " + j);
}
if ((c == 'C' || c == 'L' || c == 'D') && j != 0) {
throw new DBFException(
"The field decimal count should be 0 for character, logical, and date fields. Got: " + j);
}
if (j > i - 1) {
throw new DBFException("The field decimal count should be less than the length - 1. Got: " + j);
} else {
name = s;
type = c;
length = i;
decimalCount = j;
return;
}
private boolean validate;

public DBFField(boolean validate, String s, char c, int i, int j) throws DBFException {
this.validate = validate;
if (this.validate) {
if (s.length() > 10) {
throw new DBFException("The field name is more than 10 characters long: " + s);
}
if (c != 'C' && c != 'N' && c != 'L' && c != 'D' && c != 'F') {
throw new DBFException("The field type is not a valid. Got: " + c);
}
if (i < 1) {
throw new DBFException("The field length should be a positive integer. Got: " + i);
}
if (c == 'C' && i >= 255) {
throw new DBFException(
"The field length should be less than 255 characters for character fields. Got: " + i);
}
if (c == 'N' && i >= 21) {
throw new DBFException("The field length should be less than 21 digits for numeric fields. Got: " + i);
}
if (c == 'L' && i != 1) {
throw new DBFException("The field length should be 1 characater for logical fields. Got: " + i);
}
if (c == 'D' && i != 8) {
throw new DBFException("The field length should be 8 characaters for date fields. Got: " + i);
}
if (c == 'F' && i >= 21) {
throw new DBFException(
"The field length should be less than 21 digits for floating point fields. Got: " + i);
}
if (j < 0) {
throw new DBFException("The field decimal count should not be a negative integer. Got: " + j);
}
if ((c == 'C' || c == 'L' || c == 'D') && j != 0) {
throw new DBFException(
"The field decimal count should be 0 for character, logical, and date fields. Got: " + j);
}
if (j > i - 1) {
throw new DBFException("The field decimal count should be less than the length - 1. Got: " + j);
}
}

name = s;
type = c;
length = i;
decimalCount = j;
return;
}

public String getName() {
Expand Down
Expand Up @@ -9,26 +9,31 @@ public class DBFReader {
private DBFField fields[];
private byte nextRecord[];
private int nFieldCount;
private boolean validate;

public DBFReader(String s) throws DBFException {
public DBFReader(String s, boolean validate) throws DBFException {
stream = null;
fields = null;
nextRecord = null;
nFieldCount = 0;
this.validate = validate;

try {
init(new FileInputStream(s));
} catch (FileNotFoundException filenotfoundexception) {
throw new DBFException(filenotfoundexception);
}
}

public DBFReader(InputStream inputstream) throws DBFException {
public DBFReader(InputStream inputstream, boolean validate) throws DBFException {
stream = null;
fields = null;
nextRecord = null;
this.validate = validate;
init(inputstream);
}

@SuppressWarnings("unused")
private void init(InputStream inputstream) throws DBFException {
try {
stream = new DataInputStream(inputstream);
Expand Down Expand Up @@ -130,7 +135,7 @@ private DBFField readFieldHeader() throws IOException, DBFException {
j += 256;
if (k < 0)
k += 256;
return new DBFField(stringbuffer.toString(), c, j, k);
return new DBFField(validate, stringbuffer.toString(), c, j, k);
}

public int getFieldCount() {
Expand Down Expand Up @@ -200,4 +205,14 @@ public void close() throws DBFException {
}
}

public boolean isValidate() {
return validate;
}

public void setValidate(boolean validate) {
this.validate = validate;
}



}
Expand Up @@ -1695,3 +1695,10 @@ node.offline.error.dir=
# DatabaseOverridable: true
# Tags: other
node.offline.archive.dir=

# Determines if the *.DBF file headers should be validated when using the DBF Router
#
# DatabaseOverridable: true
# Tags: other
# Type: boolean
dbf.router.validate.header=true

0 comments on commit 3ca2b15

Please sign in to comment.