use try with resources

This commit is contained in:
Jeremy Long
2017-03-11 13:27:40 -05:00
parent 8ea6b08a0a
commit b157049a7e
2 changed files with 33 additions and 92 deletions

View File

@@ -79,38 +79,19 @@ public class HintParser {
* @throws HintParseException thrown if the XML file cannot be parsed * @throws HintParseException thrown if the XML file cannot be parsed
*/ */
public Hints parseHints(File file) throws HintParseException { public Hints parseHints(File file) throws HintParseException {
FileInputStream fis = null; //TODO there must be a better way to determine which schema to use for validation.
try { try {
fis = new FileInputStream(file); try (FileInputStream fis = new FileInputStream(file)) {
return parseHints(fis); return parseHints(fis);
} catch (IOException ex) { } catch (IOException ex) {
LOGGER.debug("", ex); LOGGER.debug("", ex);
throw new HintParseException(ex);
} catch (SAXException ex) {
try {
if (fis != null) {
try {
fis.close();
} catch (IOException ex1) {
LOGGER.debug("Unable to close stream", ex1);
}
}
fis = new FileInputStream(file);
} catch (FileNotFoundException ex1) {
throw new HintParseException(ex1);
}
try {
return parseHints(fis, HINT_SCHEMA_OLD);
} catch (SAXException ex1) {
throw new HintParseException(ex); throw new HintParseException(ex);
} }
} finally { } catch (SAXException ex) {
if (fis != null) { try (FileInputStream fis = new FileInputStream(file)) {
try { return parseHints(fis, HINT_SCHEMA_OLD);
fis.close(); } catch (SAXException | IOException ex1) {
} catch (IOException ex) { throw new HintParseException(ex);
LOGGER.debug("Unable to close stream", ex);
}
} }
} }
} }
@@ -139,23 +120,20 @@ public class HintParser {
* @throws SAXException thrown if the XML cannot be parsed * @throws SAXException thrown if the XML cannot be parsed
*/ */
private Hints parseHints(InputStream inputStream, String schema) throws HintParseException, SAXException { private Hints parseHints(InputStream inputStream, String schema) throws HintParseException, SAXException {
InputStream schemaStream = null; try (InputStream schemaStream = this.getClass().getClassLoader().getResourceAsStream(schema)) {
try {
schemaStream = this.getClass().getClassLoader().getResourceAsStream(schema);
final HintHandler handler = new HintHandler(); final HintHandler handler = new HintHandler();
final SAXParser saxParser = XmlUtils.buildSecureSaxParser(schemaStream); final SAXParser saxParser = XmlUtils.buildSecureSaxParser(schemaStream);
final XMLReader xmlReader = saxParser.getXMLReader(); final XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setErrorHandler(new HintErrorHandler()); xmlReader.setErrorHandler(new HintErrorHandler());
xmlReader.setContentHandler(handler); xmlReader.setContentHandler(handler);
try (Reader reader = new InputStreamReader(inputStream, "UTF-8")) {
final Reader reader = new InputStreamReader(inputStream, "UTF-8"); final InputSource in = new InputSource(reader);
final InputSource in = new InputSource(reader); xmlReader.parse(in);
final Hints hints = new Hints();
xmlReader.parse(in); hints.setHintRules(handler.getHintRules());
final Hints hints = new Hints(); hints.setVendorDuplicatingHintRules(handler.getVendorDuplicatingHintRules());
hints.setHintRules(handler.getHintRules()); return hints;
hints.setVendorDuplicatingHintRules(handler.getVendorDuplicatingHintRules()); }
return hints;
} catch (ParserConfigurationException | FileNotFoundException ex) { } catch (ParserConfigurationException | FileNotFoundException ex) {
LOGGER.debug("", ex); LOGGER.debug("", ex);
throw new HintParseException(ex); throw new HintParseException(ex);
@@ -169,14 +147,6 @@ public class HintParser {
} catch (IOException ex) { } catch (IOException ex) {
LOGGER.debug("", ex); LOGGER.debug("", ex);
throw new HintParseException(ex); throw new HintParseException(ex);
} finally {
if (schemaStream != null) {
try {
schemaStream.close();
} catch (IOException ex) {
LOGGER.debug("Error closing hint file stream", ex);
}
}
} }
} }
} }

View File

@@ -64,39 +64,19 @@ public class SuppressionParser {
* @throws SuppressionParseException thrown if the XML file cannot be parsed * @throws SuppressionParseException thrown if the XML file cannot be parsed
*/ */
public List<SuppressionRule> parseSuppressionRules(File file) throws SuppressionParseException { public List<SuppressionRule> parseSuppressionRules(File file) throws SuppressionParseException {
FileInputStream fis = null;
try { try {
fis = new FileInputStream(file); try (FileInputStream fis = new FileInputStream(file)) {
return parseSuppressionRules(fis); return parseSuppressionRules(fis);
} catch (IOException ex) { } catch (IOException ex) {
LOGGER.debug("", ex); LOGGER.debug("", ex);
throw new SuppressionParseException(ex); throw new SuppressionParseException(ex);
}
} catch (SAXException ex) { } catch (SAXException ex) {
try { try (FileInputStream fis = new FileInputStream(file)) {
if (fis != null) {
try {
fis.close();
} catch (IOException ex1) {
LOGGER.debug("Unable to close stream", ex1);
}
}
fis = new FileInputStream(file);
} catch (FileNotFoundException ex1) {
throw new SuppressionParseException(ex);
}
try {
return parseSuppressionRules(fis, OLD_SUPPRESSION_SCHEMA); return parseSuppressionRules(fis, OLD_SUPPRESSION_SCHEMA);
} catch (SAXException ex1) { } catch (SAXException | IOException ex1) {
throw new SuppressionParseException(ex); throw new SuppressionParseException(ex);
} }
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException ex) {
LOGGER.debug("Unable to close stream", ex);
}
}
} }
} }
@@ -124,18 +104,17 @@ public class SuppressionParser {
* @throws SAXException thrown if the XML cannot be parsed * @throws SAXException thrown if the XML cannot be parsed
*/ */
private List<SuppressionRule> parseSuppressionRules(InputStream inputStream, String schema) throws SuppressionParseException, SAXException { private List<SuppressionRule> parseSuppressionRules(InputStream inputStream, String schema) throws SuppressionParseException, SAXException {
InputStream schemaStream = null; try (InputStream schemaStream = this.getClass().getClassLoader().getResourceAsStream(schema)) {
try {
schemaStream = this.getClass().getClassLoader().getResourceAsStream(schema);
final SuppressionHandler handler = new SuppressionHandler(); final SuppressionHandler handler = new SuppressionHandler();
final SAXParser saxParser = XmlUtils.buildSecureSaxParser(schemaStream); final SAXParser saxParser = XmlUtils.buildSecureSaxParser(schemaStream);
final XMLReader xmlReader = saxParser.getXMLReader(); final XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setErrorHandler(new SuppressionErrorHandler()); xmlReader.setErrorHandler(new SuppressionErrorHandler());
xmlReader.setContentHandler(handler); xmlReader.setContentHandler(handler);
final Reader reader = new InputStreamReader(inputStream, "UTF-8"); try (Reader reader = new InputStreamReader(inputStream, "UTF-8")) {
final InputSource in = new InputSource(reader); final InputSource in = new InputSource(reader);
xmlReader.parse(in); xmlReader.parse(in);
return handler.getSuppressionRules(); return handler.getSuppressionRules();
}
} catch (ParserConfigurationException | FileNotFoundException ex) { } catch (ParserConfigurationException | FileNotFoundException ex) {
LOGGER.debug("", ex); LOGGER.debug("", ex);
throw new SuppressionParseException(ex); throw new SuppressionParseException(ex);
@@ -149,14 +128,6 @@ public class SuppressionParser {
} catch (IOException ex) { } catch (IOException ex) {
LOGGER.debug("", ex); LOGGER.debug("", ex);
throw new SuppressionParseException(ex); throw new SuppressionParseException(ex);
} finally {
if (schemaStream != null) {
try {
schemaStream.close();
} catch (IOException ex) {
LOGGER.debug("Error closing suppression file stream", ex);
}
}
} }
} }
} }