ch.hortis.sonar.mvn.mc.BaseMeasuresCollector Maven / Gradle / Ivy
/*
* This program is copyright (c) 2007 Hortis-GRC SA.
*
* This file is part of Sonar.
* Sonar is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Sonar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package ch.hortis.sonar.mvn.mc;
import ch.hortis.sonar.model.File;
import ch.hortis.sonar.model.Metric;
import ch.hortis.sonar.model.Metrics;
import ch.hortis.sonar.service.MetricService;
import ch.hortis.sonar.service.RulesService;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.project.MavenProject;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.util.Locale;
public abstract class BaseMeasuresCollector implements MeasuresCollector {
protected DecimalFormatSymbols dfs;
protected final DecimalFormat sonarFormat;
protected MetricService metricService;
protected RulesService rulesService;
protected FilesRepository filesRepository;
protected BaseMeasuresCollector() {
dfs = new DecimalFormatSymbols(Locale.ENGLISH);
// , grouping separator and . decimal separator
sonarFormat = new DecimalFormat("#.###", dfs);
}
public final boolean initialize(MavenProject project, MetricService metricService,
RulesService rulesService,
FilesRepository filesRepository) {
setMetricService(metricService);
setRuleService(rulesService);
setFilesRepository(filesRepository);
return initialize(project);
}
public FilesRepository getFilesRepository() {
return filesRepository;
}
public void setFilesRepository(FilesRepository filesRepository) {
this.filesRepository = filesRepository;
}
protected abstract boolean initialize(MavenProject project);
public MetricService getMetricService() {
return metricService;
}
public void setMetricService(MetricService metricService) {
this.metricService = metricService;
}
public RulesService getRuleService() {
return rulesService;
}
public void setRuleService(RulesService rulesService) {
this.rulesService = rulesService;
}
public File getFileFromClassName(String className) {
String fileName = StringUtils.substringAfterLast(className, ".") + ".java";
String packageName = StringUtils.substringBeforeLast(className, ".");
return filesRepository.getFile(packageName, fileName);
}
public double parseNumber(String number) throws ParseException {
if (number == null || number.trim().length() == 0) {
throw new ParseException("'" + number + "' is not a number", 0);
}
number = number.trim();
Character potentialGroupingSeparator = null;
Character potentialDecimalSeparator = null;
int potentialGroupingSeparatorCount = 0;
for (Character decimal : number.toCharArray()) {
if (!Character.isDigit(decimal) && !('E' == decimal) && !('-' == decimal)) {
if (potentialGroupingSeparator == null) {
potentialGroupingSeparator = decimal;
potentialGroupingSeparatorCount++;
} else if (potentialGroupingSeparator != null && decimal != potentialGroupingSeparator) {
potentialDecimalSeparator = decimal;
} else {
potentialGroupingSeparatorCount++;
}
}
}
if ((potentialGroupingSeparator != null && potentialDecimalSeparator != null) ||
(potentialGroupingSeparator != null && potentialGroupingSeparatorCount > 1) ||
(potentialGroupingSeparator != null && potentialGroupingSeparator == '\'')) {
throw new ParseException("'" + number + "' cannot be parsed due to grouping separators presence", 0);
}
number = number.replace(',', '.');
return sonarFormat.parse(number).doubleValue();
}
public double scaleValue(double value) {
return scaleValue(value, 2);
}
public double scaleValue(double value, int decimals) {
BigDecimal bd = new BigDecimal(value);
return bd.setScale(decimals, RoundingMode.HALF_UP).doubleValue();
}
protected Metric loadMetric(Metrics metricsEnum) {
if (getMetricService() != null) {
return getMetricService().getMetric(metricsEnum.getName());
}
return null;
}
protected java.io.File findFileFromBuildDirectory( MavenProject project, String filename ) {
String xmlOutputDirectory = project.getBuild().getDirectory() + "/" + filename;
java.io.File file = new java.io.File( xmlOutputDirectory );
if ( !file.exists() ) {
file = null;
}
return file;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy