Skip to content

Commit

Permalink
Merge pull request #290 from arunvenmany-ibm/sast_issue_fix_1.0
Browse files Browse the repository at this point in the history
reader moved to private method
  • Loading branch information
arunvenmany-ibm authored Aug 7, 2024
2 parents 9bd7e59 + aeb91b7 commit 4e74646
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,31 +61,37 @@ private static boolean hasServerRoot(File xmlFile) {

XMLEventReader reader = null;

try (FileInputStream fis = new FileInputStream(xmlFile)) {
reader = factory.createXMLEventReader(fis);
try {
reader = getReader(factory, xmlFile);
while (reader.hasNext()) {
XMLEvent nextEvent = reader.nextEvent();
if (nextEvent.isStartElement()) {
return isServerElement(nextEvent);
}
}
} catch (XMLStreamException | FileNotFoundException e) {
LOGGER.severe("Error received trying to read XML file: " + xmlFile.getAbsolutePath());
LOGGER.severe("Error received trying to read XML file: " + xmlFile.getAbsolutePath());
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception ignored) {
} catch (Exception ignored) {
}
}
}
}
} catch (Exception e) {
LOGGER.severe("Unable to access XML file "+ xmlFile.getAbsolutePath());
}

return false;
}

private static XMLEventReader getReader(XMLInputFactory factory, File xmlFile)
throws FileNotFoundException, XMLStreamException {
FileInputStream fis = new FileInputStream(xmlFile);
return factory.createXMLEventReader(fis);
}

private static XMLInputFactory getXmlInputFactory() {
XMLInputFactory factory = XMLInputFactory.newInstance();
try {
Expand All @@ -95,7 +101,6 @@ private static XMLInputFactory getXmlInputFactory() {
factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);
factory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
factory.setProperty("http://java.sun.com/xml/stream/properties/ignore-external-dtd",Boolean.TRUE);
} catch (Exception e) {
LOGGER.warning("Could not set properties on XMLInputFactory.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public static Map<String, String> getElementValues(File file, Set<String> elemen
return returnValues;
}

readElementValues(file, elementNames, returnValues);

return returnValues;
}

private static void readElementValues(File file, Set<String> elementNames, Map<String, String> returnValues) {
XMLInputFactory factory = getXmlInputFactory();
XMLEventReader reader = null;
try {
Expand All @@ -74,21 +80,19 @@ public static Map<String, String> getElementValues(File file, Set<String> elemen
}
}
} catch (XMLStreamException | FileNotFoundException e) {
LOGGER.severe("Error received trying to read XML file: " + file.getName() +
LOGGER.severe("Error received trying to read XML file: " + file.getName() +
"\n\tError" + e.getMessage());
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception ignored) {
} catch (Exception ignored) {
}
}
}
}
} catch (Exception e) {
LOGGER.severe("Unable to access XML file "+ file.getAbsolutePath());
}

return returnValues;
}

private static XMLInputFactory getXmlInputFactory() {
Expand All @@ -98,7 +102,8 @@ private static XMLInputFactory getXmlInputFactory() {
factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
factory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);

factory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
} catch (Exception e) {
LOGGER.warning("Could not set properties on XMLInputFactory.");
}
Expand Down

0 comments on commit 4e74646

Please sign in to comment.