Skip to content

Commit d0d58ca

Browse files
committed
Fixed bug in CSV format discovery.
1 parent a353e43 commit d0d58ca

File tree

9 files changed

+53
-35
lines changed

9 files changed

+53
-35
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## Layout Framework Version History
22

3+
**Changes in version 7.0**
4+
5+
- Updated minimum Java language from Java 11 to Java 21.
6+
- Fixed runtime exception being thrown when evaluating CSV format discovery.
7+
- Updated all dependencies.
8+
39
**Changes in version 6.4**
410

511
- Added support for NAACCR 26 XML.

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6.5-SNAPSHOT
1+
7.0-SNAPSHOT

build.gradle

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import com.vanniktech.maven.publish.*
33
plugins {
44
id 'java-library'
55
id 'jacoco'
6-
id 'com.vanniktech.maven.publish' version '0.34.0' // publish to Maven Central
7-
id 'com.github.ben-manes.versions' version '0.52.0' // check for out-of-date dependencies (run 'dependencyUpdates' manually)
8-
id 'com.github.spotbugs' version '6.4.2' // spotbugs code analysis
9-
id 'org.sonarqube' version '6.3.1.5724' // sonarQube analysis
6+
id 'com.vanniktech.maven.publish' version '0.35.0' // publish to Maven Central
7+
id 'com.github.ben-manes.versions' version '0.53.0' // check for out-of-date dependencies (run 'dependencyUpdates' manually)
8+
id 'com.github.spotbugs' version '6.4.8' // spotbugs code analysis
9+
id 'org.sonarqube' version '7.2.1.6560' // sonarQube analysis
1010
}
1111

1212
group = 'com.imsweb'
@@ -27,23 +27,23 @@ dependencies {
2727
api 'com.imsweb:naaccr-xml:12.1'
2828

2929
implementation 'com.thoughtworks.xstream:xstream:1.4.21'
30-
implementation 'de.siegmar:fastcsv:3.7.0'
31-
implementation 'commons-io:commons-io:2.20.0'
32-
implementation 'org.apache.commons:commons-lang3:3.18.0'
30+
implementation 'de.siegmar:fastcsv:4.1.0'
31+
implementation 'commons-io:commons-io:2.21.0'
32+
implementation 'org.apache.commons:commons-lang3:3.20.0'
3333

3434
testImplementation 'junit:junit:4.13.2'
3535
testImplementation 'com.imsweb:seerutils:5.7'
3636
testImplementation 'com.imsweb:seerutils-gui:1.20'
3737
testImplementation 'com.imsweb:naaccr-api-client:1.3' // access NAACCR documentation API
38-
testImplementation 'org.commonmark:commonmark:0.26.0' // convert markdown to HTML for NAACCR documentation
38+
testImplementation 'org.commonmark:commonmark:0.27.0' // convert markdown to HTML for NAACCR documentation
3939
testImplementation 'org.freemarker:freemarker:2.3.34' // fill it template for NAACCR documentation
4040
testImplementation('org.xhtmlrenderer:core-renderer:R8') { // Java Swing HTML renderer for NAACCR documentation lab
4141
exclude module: 'itext'
4242
}
4343
}
4444
java {
45-
sourceCompatibility = JavaVersion.VERSION_11
46-
targetCompatibility = JavaVersion.VERSION_11
45+
sourceCompatibility = JavaVersion.VERSION_21
46+
targetCompatibility = JavaVersion.VERSION_21
4747
}
4848

4949
tasks.withType(JavaCompile).configureEach {
@@ -148,7 +148,7 @@ mavenPublishing {
148148

149149
// Gradle wrapper, this allows to build the project without having to install Gradle!
150150
wrapper {
151-
gradleVersion = '8.14'
151+
gradleVersion = '9.2.1'
152152
distributionType = Wrapper.DistributionType.ALL
153153
}
154154

gradle/wrapper/gradle-wrapper.jar

181 Bytes
Binary file not shown.

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14-all.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-9.2.1-all.zip
44
networkTimeout=10000
55
validateDistributionUrl=true
66
zipStoreBase=GRADLE_USER_HOME

gradlew

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gradlew.bat

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/com/imsweb/layout/record/csv/CommaSeparatedLayout.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,12 @@ public String validateLine(String line, Integer lineNumber) {
286286
if (numFields != _numFields)
287287
msg.append("line ").append(lineNumber).append(": wrong number of fields, expected ").append(_numFields).append(" but got ").append(numFields);
288288
}
289-
catch (IOException e) {
289+
catch (IOException | RuntimeException e) {
290290
msg.append("line ").append(lineNumber).append(": ").append(e.getMessage());
291291
}
292292
}
293293

294-
return msg.length() == 0 ? null : msg.toString();
294+
return msg.isEmpty() ? null : msg.toString();
295295
}
296296

297297
@Override
@@ -384,7 +384,7 @@ public LayoutInfo buildFileInfo(String firstRecord, LayoutInfoDiscoveryOptions o
384384
result.setNumFields(getLayoutNumberOfFields());
385385
}
386386
}
387-
catch (IOException e) {
387+
catch (IOException | RuntimeException e) {
388388
// ignored, result will remain null
389389
}
390390
}
@@ -395,10 +395,13 @@ public LayoutInfo buildFileInfo(String firstRecord, LayoutInfoDiscoveryOptions o
395395
protected List<String> parseLine(String line, RecordLayoutOptions options) throws IOException {
396396
try (CsvReader<CsvRecord> reader = CsvReader.builder()
397397
.fieldSeparator(_separator)
398-
.acceptCharsAfterQuotes(options != null && options.allowCharactersAfterLastQuote())
398+
.allowExtraCharsAfterClosingQuote(options != null && options.allowCharactersAfterLastQuote())
399399
.ofCsvRecord(line)) {
400400
return reader.stream().flatMap(l -> l.getFields().stream()).collect(Collectors.toList());
401401
}
402+
catch (RuntimeException e) {
403+
throw new IOException(e.getMessage());
404+
}
402405
}
403406

404407
/**

src/test/java/com/imsweb/layout/record/csv/CommaSeparatedLayoutTest.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import java.util.HashMap;
1313
import java.util.List;
1414
import java.util.Map;
15-
import java.util.stream.Collectors;
1615

1716
import org.junit.Assert;
1817
import org.junit.Test;
@@ -37,9 +36,6 @@
3736
*/
3837
public class CommaSeparatedLayoutTest {
3938

40-
/**
41-
* Created on Jun 25, 2012 by depryf
42-
*/
4339
@Test
4440
public void testLayout() throws Exception {
4541

@@ -221,7 +217,7 @@ public void testPartialNaaccrLayout() throws Exception {
221217
// regular CSV fields don't have subfields; only fixed-column fields have those, so we have to tweak the design a bit...
222218
final Map<Integer, List<FixedColumnsField>> subFields = new HashMap<>();
223219
int idx = 1;
224-
for (String header : CsvReader.builder().ofCsvRecord(firstLine).stream().flatMap(f -> f.getFields().stream()).collect(Collectors.toList())) {
220+
for (String header : CsvReader.builder().ofCsvRecord(firstLine).stream().flatMap(f -> f.getFields().stream()).toList()) {
225221
FixedColumnsField field = null;
226222
if (header.matches("#\\d+"))
227223
field = naaccrLayout.getFieldByNaaccrItemNumber(Integer.valueOf(header.substring(1)));
@@ -251,8 +247,8 @@ public void testPartialNaaccrLayout() throws Exception {
251247
newSubField.setDefaultValue(ff.getDefaultValue());
252248
newSubField.setAlign(ff.getAlign());
253249
newSubField.setPadChar(ff.getPadChar());
254-
newSubField.setStart(ff.getStart() - field.getSubFields().get(0).getStart() + 1);
255-
newSubField.setEnd(ff.getEnd() - field.getSubFields().get(0).getStart() + 1);
250+
newSubField.setStart(ff.getStart() - field.getSubFields().getFirst().getStart() + 1);
251+
newSubField.setEnd(ff.getEnd() - field.getSubFields().getFirst().getStart() + 1);
256252
newSubFields.add(newSubField);
257253
}
258254
}
@@ -271,7 +267,7 @@ public Map<String, String> createRecordFromLine(String line, Integer lineNumber,
271267
List<FixedColumnsField> fields = subFields.get(field.getIndex());
272268
if (fields != null) {
273269
String originalValue = result.get(field.getName());
274-
if (originalValue != null && originalValue.length() >= fields.get(fields.size() - 1).getEnd()) {
270+
if (originalValue != null && originalValue.length() >= fields.getLast().getEnd()) {
275271
for (FixedColumnsField child : fields) {
276272
String value = originalValue.substring(child.getStart() - 1, child.getEnd());
277273
if (trimValues(options))
@@ -332,7 +328,7 @@ public String createLineFromRecord(Map<String, String> rec, RecordLayoutOptions
332328
LayoutFactory.registerLayout(layout);
333329

334330
// make sure we can now recognize the file
335-
LayoutInfo info = LayoutFactory.discoverFormat(file).get(0);
331+
LayoutInfo info = LayoutFactory.discoverFormat(file).getFirst();
336332
Assert.assertNotNull(info);
337333

338334
// read the data file using the new layout
@@ -343,8 +339,8 @@ public String createLineFromRecord(Map<String, String> rec, RecordLayoutOptions
343339
Assert.assertNotNull(rec.get("patientIdNumber"));
344340
Assert.assertNotNull(rec.get("race1"));
345341
}
346-
Assert.assertEquals("20100615", records.get(0).get("dateOfBirth"));
347-
Assert.assertEquals("2010", records.get(0).get("dateOfBirthYear"));
342+
Assert.assertEquals("20100615", records.getFirst().get("dateOfBirth"));
343+
Assert.assertEquals("2010", records.getFirst().get("dateOfBirthYear"));
348344
Assert.assertEquals("06", records.get(0).get("dateOfBirthMonth"));
349345
Assert.assertEquals("15", records.get(0).get("dateOfBirthDay"));
350346
Assert.assertEquals("2010", records.get(1).get("dateOfBirth"));
@@ -357,8 +353,8 @@ public String createLineFromRecord(Map<String, String> rec, RecordLayoutOptions
357353
RecordLayoutOptions options = new RecordLayoutOptions();
358354
options.setTrimValues(false);
359355
records = layout.readAllRecords(file, options);
360-
Assert.assertEquals("20100615", records.get(0).get("dateOfBirth"));
361-
Assert.assertEquals("2010", records.get(0).get("dateOfBirthYear"));
356+
Assert.assertEquals("20100615", records.getFirst().get("dateOfBirth"));
357+
Assert.assertEquals("2010", records.getFirst().get("dateOfBirthYear"));
362358
Assert.assertEquals("06", records.get(0).get("dateOfBirthMonth"));
363359
Assert.assertEquals("15", records.get(0).get("dateOfBirthDay"));
364360
Assert.assertEquals("2010 ", records.get(1).get("dateOfBirth"));
@@ -508,5 +504,18 @@ public void testBuildFileInfo() throws IOException {
508504

509505
options.setCommaSeparatedAllowDiscoveryFromNumFields(false);
510506
Assert.assertNull(layout.buildFileInfo(file, null, options));
507+
options.setCommaSeparatedAllowDiscoveryFromNumFields(true);
508+
509+
SeerUtils.writeFile("\"A\",\"B\",\"C\"", file);
510+
Assert.assertNotNull(layout.buildFileInfo(file, null, options));
511+
512+
SeerUtils.writeFile("A|B|C", file);
513+
Assert.assertNull(layout.buildFileInfo(file, null, options));
514+
515+
SeerUtils.writeFile("\"A\"|\"B\"|\"C\"", file);
516+
Assert.assertNull(layout.buildFileInfo(file, null, options));
517+
518+
SeerUtils.writeFile(" ", file);
519+
Assert.assertNull(layout.buildFileInfo(file, null, options));
511520
}
512521
}

0 commit comments

Comments
 (0)