aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch/src/main
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2015-04-02 18:13:52 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2015-04-07 17:21:33 +0200
commitb16c02760d933b45d519b2f5c1f4529aac574df5 (patch)
tree7730fc290bcf4f3fe978399d5bda328cf191f687 /sonar-batch/src/main
parenta9de737d0e2b6342c55a946665af99a35a7f2cb8 (diff)
downloadsonarqube-b16c02760d933b45d519b2f5c1f4529aac574df5.tar.gz
sonarqube-b16c02760d933b45d519b2f5c1f4529aac574df5.zip
SONAR-6277 Feed file sources in compute report
Diffstat (limited to 'sonar-batch/src/main')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/report/SourcePublisher.java68
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java1
2 files changed, 69 insertions, 0 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/SourcePublisher.java b/sonar-batch/src/main/java/org/sonar/batch/report/SourcePublisher.java
new file mode 100644
index 00000000000..ccb91681e2b
--- /dev/null
+++ b/sonar-batch/src/main/java/org/sonar/batch/report/SourcePublisher.java
@@ -0,0 +1,68 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.batch.report;
+
+import com.google.common.base.Charsets;
+import org.apache.commons.io.ByteOrderMark;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.input.BOMInputStream;
+import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.batch.index.BatchResource;
+import org.sonar.batch.index.ResourceCache;
+import org.sonar.batch.protocol.output.BatchReportWriter;
+
+import java.io.*;
+
+public class SourcePublisher implements ReportPublisherStep {
+
+ private final ResourceCache resourceCache;
+
+ public SourcePublisher(ResourceCache resourceCache) {
+ this.resourceCache = resourceCache;
+ }
+
+ @Override
+ public void publish(BatchReportWriter writer) {
+ for (final BatchResource resource : resourceCache.all()) {
+ if (!resource.isFile()) {
+ continue;
+ }
+
+ DefaultInputFile inputFile = (DefaultInputFile) resource.inputPath();
+ File iofile = writer.getSourceFile(1);
+ int line = 0;
+ try (FileOutputStream output = new FileOutputStream(iofile); BOMInputStream bomIn = new BOMInputStream(new FileInputStream(inputFile.file()),
+ ByteOrderMark.UTF_8, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_32LE, ByteOrderMark.UTF_32BE);
+ BufferedReader reader = new BufferedReader(new InputStreamReader(bomIn, inputFile.charset()))) {
+ String lineStr = reader.readLine();
+ while (lineStr != null) {
+ IOUtils.write(lineStr, output, Charsets.UTF_8);
+ line++;
+ if (line < inputFile.lines()) {
+ IOUtils.write("\n", output, Charsets.UTF_8);
+ }
+ lineStr = reader.readLine();
+ }
+ } catch (IOException e) {
+ throw new IllegalStateException("Unable to store file source in the report", e);
+ }
+ }
+ }
+}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java
index 43ba3500126..12b0bc0f21d 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java
@@ -188,6 +188,7 @@ public class ProjectScanContainer extends ComponentContainer {
MeasuresPublisher.class,
DuplicationsPublisher.class,
CoveragePublisher.class,
+ SourcePublisher.class,
ScanTaskObservers.class);
}