You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LineMeasureSensor.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.xoo.lang;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.util.List;
  24. import java.util.Map;
  25. import org.apache.commons.io.FileUtils;
  26. import org.apache.commons.lang.StringUtils;
  27. import org.sonar.api.batch.fs.InputFile;
  28. import org.sonar.api.batch.sensor.Sensor;
  29. import org.sonar.api.batch.sensor.SensorContext;
  30. import org.sonar.api.batch.sensor.SensorDescriptor;
  31. import org.sonar.api.measures.FileLinesContext;
  32. import org.sonar.api.measures.FileLinesContextFactory;
  33. import org.sonar.api.utils.KeyValueFormat;
  34. import org.sonar.api.utils.log.Logger;
  35. import org.sonar.api.utils.log.Loggers;
  36. import org.sonar.xoo.Xoo;
  37. /**
  38. * Parse files *.xoo.measures
  39. */
  40. public class LineMeasureSensor implements Sensor {
  41. private static final Logger LOG = Loggers.get(LineMeasureSensor.class);
  42. private static final String MEASURES_EXTENSION = ".linemeasures";
  43. private FileLinesContextFactory contextFactory;
  44. public LineMeasureSensor(FileLinesContextFactory contextFactory) {
  45. this.contextFactory = contextFactory;
  46. }
  47. private void processFileMeasures(InputFile inputFile, SensorContext context) {
  48. File ioFile = inputFile.file();
  49. File measureFile = new File(ioFile.getParentFile(), ioFile.getName() + MEASURES_EXTENSION);
  50. if (measureFile.exists()) {
  51. LOG.debug("Processing " + measureFile.getAbsolutePath());
  52. try {
  53. FileLinesContext linesContext = contextFactory.createFor(inputFile);
  54. List<String> lines = FileUtils.readLines(measureFile, context.fileSystem().encoding().name());
  55. int lineNumber = 0;
  56. for (String line : lines) {
  57. lineNumber++;
  58. if (StringUtils.isBlank(line) || line.startsWith("#")) {
  59. continue;
  60. }
  61. processMeasure(linesContext, measureFile, lineNumber, line);
  62. }
  63. linesContext.save();
  64. } catch (IOException e) {
  65. throw new IllegalStateException(e);
  66. }
  67. }
  68. }
  69. private void processMeasure(FileLinesContext context, File measureFile, int lineNumber, String line) {
  70. try {
  71. String metricKey = StringUtils.substringBefore(line, ":");
  72. String value = line.substring(metricKey.length() + 1);
  73. saveMeasure(context, metricKey, KeyValueFormat.parseIntInt(value));
  74. } catch (Exception e) {
  75. LOG.error("Error processing line " + lineNumber + " of file " + measureFile.getAbsolutePath(), e);
  76. throw new IllegalStateException("Error processing line " + lineNumber + " of file " + measureFile.getAbsolutePath(), e);
  77. }
  78. }
  79. private void saveMeasure(FileLinesContext context, String metricKey, Map<Integer, Integer> values) {
  80. for (Map.Entry<Integer, Integer> entry : values.entrySet()) {
  81. context.setIntValue(metricKey, entry.getKey(), entry.getValue());
  82. }
  83. }
  84. @Override
  85. public void describe(SensorDescriptor descriptor) {
  86. descriptor
  87. .name("Xoo Line Measure Sensor")
  88. .onlyOnLanguages(Xoo.KEY);
  89. }
  90. @Override
  91. public void execute(SensorContext context) {
  92. for (InputFile file : context.fileSystem().inputFiles(context.fileSystem().predicates().hasLanguages(Xoo.KEY))) {
  93. processFileMeasures(file, context);
  94. }
  95. }
  96. }