Skip to content

Commit

Permalink
Ensure cflint config and result files get deleted after done
Browse files Browse the repository at this point in the history
  • Loading branch information
nbihan-mediware committed Dec 17, 2018
1 parent 038ee3c commit 8f274b1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>com.stepstone.sonar.plugin</groupId>
<artifactId>sonar-coldfusion-plugin</artifactId>
<packaging>sonar-plugin</packaging>
<version>1.6.5-SNAPSHOT</version>
<version>1.6.6-SNAPSHOT</version>

<name>SonarQube Coldfusion Analyzer</name>
<description>Enables scanning of ColdFusion source files</description>
Expand Down Expand Up @@ -39,7 +39,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<sonar.version>6.7.5</sonar.version>
<sonar.version>6.7.6</sonar.version>
<cflint.version>1.2.3</cflint.version>
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
name = "File suffixes",
description = "Comma-separated list of suffixes of files to analyze.",
project = true,
multiValues = true,
global = true
),
@Property(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import javax.xml.stream.XMLStreamException;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

public class ColdFusionSensor implements Sensor {

Expand Down Expand Up @@ -63,21 +64,32 @@ public void execute(SensorContext context) {
}
}

protected void analyze(SensorContext context) throws IOException, XMLStreamException {
new CFLintAnalyzer(context).analyze(generateCflintConfig());
private void analyze(SensorContext context) throws IOException, XMLStreamException {
File configFile = generateCflintConfig();
new CFLintAnalyzer(context).analyze(configFile);
//when analysis is done we delete the created file
deleteFile(configFile);
}

protected File generateCflintConfig() throws IOException, XMLStreamException {
private File generateCflintConfig() throws IOException, XMLStreamException {
final File configFile = new File(fs.workDir(), "cflint-config.xml");
new CFlintConfigExporter(ruleProfile).save(configFile);
return configFile;
}

protected void importResults(SensorContext sensorContext) {
private void deleteFile(File configFile) throws IOException {
if(configFile!= null){
Files.deleteIfExists(configFile.toPath());
}
}

private void importResults(SensorContext sensorContext) throws IOException {
try {
new CFlintAnalysisResultImporter(fs, sensorContext).parse(new File(fs.workDir(), "cflint-result.xml"));
} catch (IOException | XMLStreamException e) {
} catch (XMLStreamException e) {
LOGGER.error(",e");
} finally {
deleteFile(new File(fs.workDir(), "cflint-result.xml"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,26 @@ public void save(File configFile) throws IOException, XMLStreamException {

public void save(Writer writer) throws IOException, XMLStreamException {
final XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
XMLStreamWriter xtw = xmlOutputFactory.createXMLStreamWriter(writer);
XMLStreamWriter xtw=null;
try {
xtw = xmlOutputFactory.createXMLStreamWriter(writer);

xtw.writeStartDocument();
xtw.writeStartElement("config");
xtw.writeStartDocument();
xtw.writeStartElement("config");

for (ActiveRule activeRule : ruleProfile.getActiveRulesByRepository(repositoryKey)) {
xtw.writeStartElement("includes");
xtw.writeAttribute("code", activeRule.getRuleKey());
xtw.writeEndElement();
}

for (ActiveRule activeRule : ruleProfile.getActiveRulesByRepository(repositoryKey)) {
xtw.writeStartElement("includes");
xtw.writeAttribute("code", activeRule.getRuleKey());
xtw.writeEndElement();
xtw.writeEndDocument();
} finally {
if(xtw!=null) {
xtw.close();
}
}

xtw.writeEndElement();
xtw.writeEndDocument();
xtw.close();
}
}

0 comments on commit 8f274b1

Please sign in to comment.