Skip to content

Commit

Permalink
Fixes for findbugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
depryf committed Nov 30, 2015
1 parent 561bc13 commit 54c6186
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 71 deletions.
10 changes: 10 additions & 0 deletions config/findbugs/findbugs-exclude.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
<FindBugsFilter>
<Match>
<Ckass nane="com.imsweb.naaccrxml.gui.Standalone"/>
<Bug pattern="DM_EXIT"/>
</Match>
<Match>
<Bug pattern="EI_EXPOSE_REP"/>
</Match>
<Match>
<Bug pattern="EI_EXPOSE_REP2"/>
</Match>
</FindBugsFilter>
20 changes: 12 additions & 8 deletions src/main/java/com/imsweb/naaccrxml/BatchProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
package com.imsweb.naaccrxml;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -102,7 +107,7 @@ public static void main(String[] args) throws IOException, InterruptedException
}
int numThreads = Math.min(Runtime.getRuntime().availableProcessors() + 1, 5);
if (opt.getProperty(_OPTION_PROCESSING_NUM_THREADS) != null && !opt.getProperty(_OPTION_PROCESSING_NUM_THREADS).isEmpty())
numThreads = Integer.valueOf(opt.getProperty(_OPTION_PROCESSING_NUM_THREADS));
numThreads = Integer.parseInt(opt.getProperty(_OPTION_PROCESSING_NUM_THREADS));
if (opt.getProperty(_OPTION_OUTPUT_FOLDER) == null || opt.getProperty(_OPTION_OUTPUT_FOLDER).isEmpty())
throw new RuntimeException("Option " + _OPTION_OUTPUT_FOLDER + " is required.");
String compression = opt.getProperty(_OPTION_PROCESSING_COMPRESSION);
Expand Down Expand Up @@ -162,7 +167,7 @@ public static void main(String[] args) throws IOException, InterruptedException

// write the report
if (createReport) {
FileWriter reportWriter = new FileWriter(new File(outputDir, reportName));
Writer reportWriter = new OutputStreamWriter(new FileOutputStream(new File(outputDir, reportName)), StandardCharsets.UTF_8);

reportWriter.write("Report created on " + new Date() + "\n\n");
reportWriter.write("total number of files: " + formatNumber(toProcess.size()) + "\n");
Expand Down Expand Up @@ -206,11 +211,9 @@ private static Properties readOptions(String[] args) {
if (args.length != 0) {
File file = new File(args[0]);
if (file.exists()) {
try {
FileReader reader = new FileReader(file);
try (Reader reader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)) {
opt = new Properties();
opt.load(reader);
reader.close();
}
catch (IOException e) {
opt = null;
Expand Down Expand Up @@ -334,7 +337,8 @@ public void run() {
}

if (_deleteOutputFiles)
_outputFile.delete();
if (!_outputFile.delete())
System.err.println("Unable to delete " + _outputFile.getPath());
}
}

Expand Down
38 changes: 19 additions & 19 deletions src/main/java/com/imsweb/naaccrxml/NaaccrFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
public class NaaccrFormat {

// version constants
public static String NAACCR_VERSION_160 = "160";
public static String NAACCR_VERSION_150 = "150";
public static String NAACCR_VERSION_140 = "140";
public static final String NAACCR_VERSION_160 = "160";
public static final String NAACCR_VERSION_150 = "150";
public static final String NAACCR_VERSION_140 = "140";

// list of supported versions
private static final List<String> _SUPPORTED_VERSIONS = new ArrayList<>();
Expand All @@ -36,18 +36,18 @@ public static Set<String> getSupportedVersions() {
}

// format constants
public static String NAACCR_FORMAT_16_ABSTRACT = "naaccr-160-abstract";
public static String NAACCR_FORMAT_16_MODIFIED = "naaccr-160-modified";
public static String NAACCR_FORMAT_16_CONFIDENTIAL = "naaccr-160-confidential";
public static String NAACCR_FORMAT_16_INCIDENCE = "naaccr-160-incidence";
public static String NAACCR_FORMAT_15_ABSTRACT = "naaccr-150-abstract";
public static String NAACCR_FORMAT_15_MODIFIED = "naaccr-150-modified";
public static String NAACCR_FORMAT_15_CONFIDENTIAL = "naaccr-150-confidential";
public static String NAACCR_FORMAT_15_INCIDENCE = "naaccr-150-incidence";
public static String NAACCR_FORMAT_14_ABSTRACT = "naaccr-140-abstract";
public static String NAACCR_FORMAT_14_MODIFIED = "naaccr-140-modified";
public static String NAACCR_FORMAT_14_CONFIDENTIAL = "naaccr-140-confidential";
public static String NAACCR_FORMAT_14_INCIDENCE = "naaccr-140-incidence";
public static final String NAACCR_FORMAT_16_ABSTRACT = "naaccr-160-abstract";
public static final String NAACCR_FORMAT_16_MODIFIED = "naaccr-160-modified";
public static final String NAACCR_FORMAT_16_CONFIDENTIAL = "naaccr-160-confidential";
public static final String NAACCR_FORMAT_16_INCIDENCE = "naaccr-160-incidence";
public static final String NAACCR_FORMAT_15_ABSTRACT = "naaccr-150-abstract";
public static final String NAACCR_FORMAT_15_MODIFIED = "naaccr-150-modified";
public static final String NAACCR_FORMAT_15_CONFIDENTIAL = "naaccr-150-confidential";
public static final String NAACCR_FORMAT_15_INCIDENCE = "naaccr-150-incidence";
public static final String NAACCR_FORMAT_14_ABSTRACT = "naaccr-140-abstract";
public static final String NAACCR_FORMAT_14_MODIFIED = "naaccr-140-modified";
public static final String NAACCR_FORMAT_14_CONFIDENTIAL = "naaccr-140-confidential";
public static final String NAACCR_FORMAT_14_INCIDENCE = "naaccr-140-incidence";

// list of supported formats
private static final List<String> _SUPPORTED_FORMATS = new ArrayList<>();
Expand Down Expand Up @@ -76,10 +76,10 @@ public static Set<String> getSupportedFormats() {
}

// record type constants
public static String NAACCR_REC_TYPE_ABSTRACT = "A";
public static String NAACCR_REC_TYPE_MODIFIED = "M";
public static String NAACCR_REC_TYPE_CONFIDENTIAL = "C";
public static String NAACCR_REC_TYPE_INCIDENCE = "I";
public static final String NAACCR_REC_TYPE_ABSTRACT = "A";
public static final String NAACCR_REC_TYPE_MODIFIED = "M";
public static final String NAACCR_REC_TYPE_CONFIDENTIAL = "C";
public static final String NAACCR_REC_TYPE_INCIDENCE = "I";

// list of supported record types
private static final List<String> _SUPPORTED_REC_TYPES = new ArrayList<>();
Expand Down
47 changes: 30 additions & 17 deletions src/main/java/com/imsweb/naaccrxml/NaaccrXmlDictionaryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
package com.imsweb.naaccrxml;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -44,15 +46,15 @@ public final class NaaccrXmlDictionaryUtils {
public static final String NAACCR_DATA_TYPE_DATE = "date"; // digits, YYYY or YYYYMM or YYYYMMDD

// regular expression for each data type
public static final Map<String, Pattern> NAACCR_DATA_TYPES_REGEX = new HashMap<>();
private static final Map<String, Pattern> _NAACCR_DATA_TYPES_REGEX = new HashMap<>();

static {
NAACCR_DATA_TYPES_REGEX.put(NAACCR_DATA_TYPE_ALPHA, Pattern.compile("^[A-Z]+$"));
NAACCR_DATA_TYPES_REGEX.put(NAACCR_DATA_TYPE_DIGITS, Pattern.compile("^[0-9]+$"));
NAACCR_DATA_TYPES_REGEX.put(NAACCR_DATA_TYPE_MIXED, Pattern.compile("^[A-Z0-9]+$"));
NAACCR_DATA_TYPES_REGEX.put(NAACCR_DATA_TYPE_NUMERIC, Pattern.compile("^[0-9]+(\\.[0-9]+)?$"));
NAACCR_DATA_TYPES_REGEX.put(NAACCR_DATA_TYPE_TEXT, Pattern.compile("^.+$"));
NAACCR_DATA_TYPES_REGEX.put(NAACCR_DATA_TYPE_DATE, Pattern.compile("^(18|19|20)[0-9][0-9]((0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])?)?$"));
_NAACCR_DATA_TYPES_REGEX.put(NAACCR_DATA_TYPE_ALPHA, Pattern.compile("^[A-Z]+$"));
_NAACCR_DATA_TYPES_REGEX.put(NAACCR_DATA_TYPE_DIGITS, Pattern.compile("^[0-9]+$"));
_NAACCR_DATA_TYPES_REGEX.put(NAACCR_DATA_TYPE_MIXED, Pattern.compile("^[A-Z0-9]+$"));
_NAACCR_DATA_TYPES_REGEX.put(NAACCR_DATA_TYPE_NUMERIC, Pattern.compile("^[0-9]+(\\.[0-9]+)?$"));
_NAACCR_DATA_TYPES_REGEX.put(NAACCR_DATA_TYPE_TEXT, Pattern.compile("^.+$"));
_NAACCR_DATA_TYPES_REGEX.put(NAACCR_DATA_TYPE_DATE, Pattern.compile("^(18|19|20)[0-9][0-9]((0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])?)?$"));
}

// trimming rules (default is all)
Expand All @@ -74,15 +76,24 @@ public final class NaaccrXmlDictionaryUtils {
private NaaccrXmlDictionaryUtils() {
}

/**
* Returns the pattern for the provided data type, null if not found.
* @param dataType requested data type
* @return corresponding pattern, maybe null
*/
public static Pattern getDataTypePattern(String dataType) {
return _NAACCR_DATA_TYPES_REGEX.get(dataType);
}

/**
* Returns whether values for a given data type need to have the same length as their definition
* @param type given data type
* @return true if the values of that type needs to be fully filled-in
*/
public static boolean isFullLengthRequiredForType(String type) {
boolean result = NaaccrXmlDictionaryUtils.NAACCR_DATA_TYPE_ALPHA.equals(type);
result |= NaaccrXmlDictionaryUtils.NAACCR_DATA_TYPE_DIGITS.equals(type);
result |= NaaccrXmlDictionaryUtils.NAACCR_DATA_TYPE_MIXED.equals(type);
boolean result = NAACCR_DATA_TYPE_ALPHA.equals(type);
result |= NAACCR_DATA_TYPE_DIGITS.equals(type);
result |= NAACCR_DATA_TYPE_MIXED.equals(type);
return result;
}

Expand Down Expand Up @@ -135,7 +146,8 @@ public static NaaccrDictionary getBaseDictionaryByVersion(String naaccrVersion)
throw new RuntimeException("Version is required for getting the base dictionary.");
if (!NaaccrFormat.isVersionSupported(naaccrVersion))
throw new RuntimeException("Unsupported base dictionary version: " + naaccrVersion);
try (Reader reader = new InputStreamReader(Thread.currentThread().getContextClassLoader().getResource("naaccr-dictionary-" + naaccrVersion + ".xml").openStream())) {
String resName = "naaccr-dictionary-" + naaccrVersion + ".xml";
try (Reader reader = new InputStreamReader(Thread.currentThread().getContextClassLoader().getResource(resName).openStream(), StandardCharsets.UTF_8)) {
return readDictionary(reader);
}
catch (IOException e) {
Expand Down Expand Up @@ -165,7 +177,8 @@ public static NaaccrDictionary getDefaultUserDictionaryByVersion(String naaccrVe
throw new RuntimeException("Version is required for getting the default user dictionary.");
if (!NaaccrFormat.isVersionSupported(naaccrVersion))
throw new RuntimeException("Unsupported default user dictionary version: " + naaccrVersion);
try (Reader reader = new InputStreamReader(Thread.currentThread().getContextClassLoader().getResource("user-defined-naaccr-dictionary-" + naaccrVersion + ".xml").openStream())) {
String resName = "user-defined-naaccr-dictionary-" + naaccrVersion + ".xml";
try (Reader reader = new InputStreamReader(Thread.currentThread().getContextClassLoader().getResource(resName).openStream(), StandardCharsets.UTF_8)) {
return readDictionary(reader);
}
catch (IOException e) {
Expand All @@ -184,7 +197,7 @@ public static NaaccrDictionary readDictionary(File file) throws IOException {
throw new IOException("File is required to load dictionary.");
if (!file.exists())
throw new IOException("File must exist to load dictionary.");
try (FileReader reader = new FileReader(file)) {
try (Reader reader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)) {
return readDictionary(reader);
}
}
Expand All @@ -211,7 +224,7 @@ public static NaaccrDictionary readDictionary(Reader reader) throws IOException
* @throws IOException if the dictionary could not be written
*/
public static void writeDictionary(NaaccrDictionary dictionary, File file) throws IOException {
try (FileWriter writer = new FileWriter(file)) {
try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
writeDictionary(dictionary, writer);
}
}
Expand Down Expand Up @@ -426,7 +439,7 @@ public void addAttribute(String key, String value) {
/**
* This complex logic is needed because we wanted a non-standard formatting for the XML (in terms of indentations)...
* @param attribute attribute to consider
* @return true if the provided attribute is the last one on the line, false otherwise.
* @return true if the provided attribute is the last one on the line, false otherwise.
*/
private boolean isLastAttribute(String attribute) {
NaaccrDictionaryItem item = _dictionary.getItemByNaaccrId(_currentItemId);
Expand Down
22 changes: 20 additions & 2 deletions src/main/java/com/imsweb/naaccrxml/NaaccrXmlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,9 @@ public static String getFormatFromXmlReader(Reader xmlReader) {
* @throws NaaccrIOException if the reader cannot be created
*/
public static Reader createReader(File file) throws NaaccrIOException {
InputStream is = null;
try {
InputStream is = new FileInputStream(file);
is = new FileInputStream(file);

if (file.getName().endsWith(".gz"))
is = new GZIPInputStream(is);
Expand All @@ -331,6 +332,14 @@ else if (file.getName().endsWith(".xz"))
return new InputStreamReader(is, StandardCharsets.UTF_8);
}
catch (IOException e) {
if (is != null) {
try {
is.close();
}
catch (IOException e1) {
// give up
}
}
throw new NaaccrIOException(e.getMessage());
}
}
Expand All @@ -342,8 +351,9 @@ else if (file.getName().endsWith(".xz"))
* @throws NaaccrIOException if the writer cannot be created
*/
public static Writer createWriter(File file) throws NaaccrIOException {
OutputStream os = null;
try {
OutputStream os = new FileOutputStream(file);
os = new FileOutputStream(file);

if (file.getName().endsWith(".gz"))
os = new GZIPOutputStream(os);
Expand All @@ -353,6 +363,14 @@ else if (file.getName().endsWith(".xz"))
return new OutputStreamWriter(os, StandardCharsets.UTF_8);
}
catch (IOException e) {
if (os != null) {
try {
os.close();
}
catch (IOException e1) {
// give up
}
}
throw new NaaccrIOException(e.getMessage());
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/com/imsweb/naaccrxml/PatientFlatReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@ public PatientFlatReader(Reader reader, NaaccrOptions options, NaaccrDictionary
throw new NaaccrIOException("invalid line length for first record, expected " + _format.getLineLength() + " but got " + _previousLine.length());

// read the root items
if (_previousLine != null)
for (RuntimeNaaccrDictionaryItem itemDef : _dictionary.getItems())
if (NaaccrXmlUtils.NAACCR_XML_TAG_ROOT.equals(itemDef.getParentXmlElement()))
addItemFromLine(_rootData, _previousLine, _reader.getLineNumber(), itemDef);
for (RuntimeNaaccrDictionaryItem itemDef : _dictionary.getItems())
if (NaaccrXmlUtils.NAACCR_XML_TAG_ROOT.equals(itemDef.getParentXmlElement()))
addItemFromLine(_rootData, _previousLine, _reader.getLineNumber(), itemDef);

// let's cache the grouping items, we are going to need them a lot...
_groupingItems = new ArrayList<>();
Expand Down Expand Up @@ -236,7 +235,7 @@ protected Item createItemFromLine(AbstractEntity entity, String line, Integer li
reportError(entity, lineNumber, def, item.getValue(), NaaccrErrorUtils.CODE_VAL_TOO_LONG, def.getLength(), item.getValue().length());
else if (NaaccrXmlDictionaryUtils.isFullLengthRequiredForType(def.getDataType()) && item.getValue().length() != def.getLength())
reportError(entity, lineNumber, def, item.getValue(), NaaccrErrorUtils.CODE_VAL_TOO_SHORT, def.getLength(), item.getValue().length());
else if (def.getDataType() != null && !NaaccrXmlDictionaryUtils.NAACCR_DATA_TYPES_REGEX.get(def.getDataType()).matcher(item.getValue()).matches())
else if (def.getDataType() != null && !NaaccrXmlDictionaryUtils.getDataTypePattern(def.getDataType()).matcher(item.getValue()).matches())
reportError(entity, lineNumber, def, item.getValue(), NaaccrErrorUtils.CODE_VAL_DATA_TYPE, def.getDataType());
else if (def.getRegexValidation() != null && !def.getRegexValidation().matcher(item.getValue()).matches())
reportError(entity, lineNumber, def, item.getValue(), NaaccrErrorUtils.CODE_VAL_DATA_TYPE, def.getRegexValidation());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ private void performProcessing(final File srcFile, final long analysisTime) {
_northProcessingLayout.show(_northProcessingPnl, _NORTH_PROCESSING_PANEL_ID_PROGRESS);

_processingBar.setMinimum(0);
_processingBar.setMaximum(Integer.valueOf(_numLinesLbl.getText().replaceAll(",", "")));
_processingBar.setMaximum(Integer.parseInt(_numLinesLbl.getText().replaceAll(",", "")));
_processingBar.setValue(0);

_maxWarningsReached = _maxWarningsDiscAdded = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ else if (NaaccrOptions.ITEM_HANDLING_IGNORE.equals(_context.getOptions().getUnkn
reportError(entity, lineNumber, currentPath, def, item.getValue(), NaaccrErrorUtils.CODE_VAL_TOO_LONG, def.getLength(), item.getValue().length());
else if (NaaccrXmlDictionaryUtils.isFullLengthRequiredForType(def.getDataType()) && item.getValue().length() != def.getLength())
reportError(entity, lineNumber, currentPath, def, item.getValue(), NaaccrErrorUtils.CODE_VAL_TOO_SHORT, def.getLength(), item.getValue().length());
else if (def.getDataType() != null && !NaaccrXmlDictionaryUtils.NAACCR_DATA_TYPES_REGEX.get(def.getDataType()).matcher(item.getValue()).matches())
else if (def.getDataType() != null && !NaaccrXmlDictionaryUtils.getDataTypePattern(def.getDataType()).matcher(item.getValue()).matches())
reportError(entity, lineNumber, currentPath, def, item.getValue(), NaaccrErrorUtils.CODE_VAL_DATA_TYPE, def.getDataType());
else if (def.getRegexValidation() != null && !def.getRegexValidation().matcher(item.getValue()).matches())
reportError(entity, lineNumber, currentPath, def, item.getValue(), NaaccrErrorUtils.CODE_VAL_DATA_TYPE, def.getRegexValidation());
Expand Down
Loading

0 comments on commit 54c6186

Please sign in to comment.