fix sonar qube finding for try with resources and dtd external processing

This commit is contained in:
Sebastian Sdorra
2020-03-11 15:55:25 +01:00
parent 3c6ae8e320
commit c4bf157075

View File

@@ -50,13 +50,8 @@ import sonia.scm.plugin.PluginAnnotation;
//~--- JDK imports ------------------------------------------------------------ //~--- JDK imports ------------------------------------------------------------
import java.io.Closeable;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
@@ -89,11 +84,13 @@ import javax.tools.StandardLocation;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.ext.Provider; import javax.ws.rs.ext.Provider;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys; import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer; import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
@@ -160,18 +157,6 @@ public final class ScmAnnotationProcessor extends AbstractProcessor {
return false; return false;
} }
private void close(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException ex) {
printException("could not close closeable", ex);
}
}
}
private TypeElement findAnnotation(Set<? extends TypeElement> annotations, private TypeElement findAnnotation(Set<? extends TypeElement> annotations,
Class<? extends Annotation> annotationClass) { Class<? extends Annotation> annotationClass) {
TypeElement annotation = null; TypeElement annotation = null;
@@ -205,15 +190,12 @@ public final class ScmAnnotationProcessor extends AbstractProcessor {
private Document parseDocument(File file) { private Document parseDocument(File file) {
Document doc = null; Document doc = null;
InputStream input = null;
try { try {
DocumentBuilder builder = DocumentBuilder builder = createDocumentBuilder();
DocumentBuilderFactory.newInstance().newDocumentBuilder();
if (file.exists()) { if (file.exists()) {
input = new FileInputStream(file); doc = builder.parse(file);
doc = builder.parse(input);
} else { } else {
doc = builder.newDocument(); doc = builder.newDocument();
doc.appendChild(doc.createElement(EL_MODULE)); doc.appendChild(doc.createElement(EL_MODULE));
@@ -221,13 +203,17 @@ public final class ScmAnnotationProcessor extends AbstractProcessor {
} catch (ParserConfigurationException | SAXException | IOException } catch (ParserConfigurationException | SAXException | IOException
| DOMException ex) { | DOMException ex) {
printException("could not parse document", ex); printException("could not parse document", ex);
} finally {
close(input);
} }
return doc; return doc;
} }
private DocumentBuilder createDocumentBuilder() throws ParserConfigurationException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
return factory.newDocumentBuilder();
}
private String prepareArrayElement(Object obj) { private String prepareArrayElement(Object obj) {
String v = obj.toString(); String v = obj.toString();
@@ -341,24 +327,25 @@ public final class ScmAnnotationProcessor extends AbstractProcessor {
private void writeDocument(Document doc, File file) { private void writeDocument(Document doc, File file) {
Writer writer = null;
try { try {
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
writer = new FileWriter(file);
Transformer transformer = Transformer transformer = createTransformer();
TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, PROPERTY_VALUE); transformer.setOutputProperty(OutputKeys.INDENT, PROPERTY_VALUE);
transformer.transform(new DOMSource(doc), new StreamResult(writer)); transformer.transform(new DOMSource(doc), new StreamResult(file));
} catch (IOException | IllegalArgumentException | TransformerException ex) { } catch (IllegalArgumentException | TransformerException ex) {
printException("could not write document", ex); printException("could not write document", ex);
} finally {
close(writer);
} }
} }
private Transformer createTransformer() throws TransformerConfigurationException {
TransformerFactory factory = TransformerFactory.newInstance();
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
return factory.newTransformer();
}
private Map<String, String> getAttributesFromAnnotation(Element el, private Map<String, String> getAttributesFromAnnotation(Element el,
TypeElement annotation) { TypeElement annotation) {