parent
c7b633deab
commit
0061c15772
@ -0,0 +1,69 @@ |
||||
/* |
||||
* Copyright (c) 2023 - 2024. KeyWare.Co.Ltd All rights reserved. |
||||
* 项目名称:信息安全性设计准则检查插件 |
||||
* 项目描述:用于检查源代码的安全性设计准则的Sonarqube插件 |
||||
* 版权说明:本软件属北京关键科技股份有限公司所有,在未获得北京关键科技股份有限公司正式授权情况下,任何企业和个人,不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。 |
||||
*/ |
||||
package com.keyware.sonar.java; |
||||
|
||||
import com.keyware.sonar.java.rules.checkers.ConfigurationFileChecker; |
||||
import org.sonar.api.batch.fs.FilePredicates; |
||||
import org.sonar.api.batch.fs.InputFile; |
||||
import org.sonar.api.batch.rule.CheckFactory; |
||||
import org.sonar.api.batch.rule.Checks; |
||||
import org.sonar.api.batch.sensor.Sensor; |
||||
import org.sonar.api.batch.sensor.SensorContext; |
||||
import org.sonar.api.batch.sensor.SensorDescriptor; |
||||
import org.sonar.api.measures.CoreMetrics; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
public class ConfigFileSquidSensor implements Sensor { |
||||
private final Checks<ConfigurationFileChecker> checks; |
||||
|
||||
private SensorContext context; |
||||
|
||||
public ConfigFileSquidSensor(CheckFactory checkFactory){ |
||||
checks = checkFactory.create("config"); |
||||
checks.addAnnotatedChecks(ConfigurationFileChecker.class); |
||||
} |
||||
@Override |
||||
public void describe(SensorDescriptor descriptor) { |
||||
descriptor |
||||
.name("ConfigSquidSensor") |
||||
.onlyOnLanguages(ConfigurationFileLanguage.KEY) |
||||
.createIssuesForRuleRepository("config"); |
||||
} |
||||
|
||||
@Override |
||||
public void execute(SensorContext context) { |
||||
FilePredicates p = context.fileSystem().predicates(); |
||||
for (InputFile inputFile : context.fileSystem().inputFiles(p.hasLanguages(ConfigurationFileLanguage.KEY))) { |
||||
checks.all().forEach(check -> { |
||||
check.execute(context, inputFile, checks.ruleKey(check)); |
||||
}); |
||||
} |
||||
} |
||||
|
||||
private String readFileContents(InputFile javaFile) { |
||||
String content; |
||||
try { |
||||
content = javaFile.contents(); |
||||
} catch (IOException e) { |
||||
System.err.println("Failed to read " + javaFile + " due to " + e.getMessage()); |
||||
content = ""; |
||||
} |
||||
return content; |
||||
} |
||||
|
||||
private void measureLines(SensorContext context, InputFile javaFile, String content) { |
||||
int lines = content.split("[\n\r]").length; |
||||
context.<Integer>newMeasure() |
||||
.forMetric(CoreMetrics.NCLOC) |
||||
.on(javaFile) |
||||
.withValue(lines) |
||||
.save(); |
||||
} |
||||
|
||||
|
||||
} |
Loading…
Reference in new issue