2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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.
11 * SonarQube 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.
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.
20 package org.sonar.server.computation.step;
22 import org.sonar.batch.protocol.output.BatchReport;
23 import org.sonar.core.util.CloseableIterator;
24 import org.sonar.server.computation.batch.BatchReportReader;
25 import org.sonar.server.computation.component.Component;
26 import org.sonar.server.computation.component.CrawlerDepthLimit;
27 import org.sonar.server.computation.component.DepthTraversalTypeAwareCrawler;
28 import org.sonar.server.computation.component.ReportTreeRootHolder;
29 import org.sonar.server.computation.component.TypeAwareVisitorAdapter;
30 import org.sonar.server.computation.duplication.DuplicationRepository;
31 import org.sonar.server.computation.duplication.TextBlock;
33 import static org.sonar.server.computation.component.ComponentVisitor.Order.POST_ORDER;
36 * Loads duplication information from the report and loads them into the {@link DuplicationRepository}.
38 public class LoadDuplicationsFromReportStep implements ComputationStep {
39 private final ReportTreeRootHolder treeRootHolder;
40 private final BatchReportReader batchReportReader;
41 private final DuplicationRepository duplicationRepository;
43 public LoadDuplicationsFromReportStep(ReportTreeRootHolder treeRootHolder, BatchReportReader batchReportReader, DuplicationRepository duplicationRepository) {
44 this.treeRootHolder = treeRootHolder;
45 this.batchReportReader = batchReportReader;
46 this.duplicationRepository = duplicationRepository;
50 public String getDescription() {
51 return "Load inner file and in project duplications";
55 public void execute() {
56 new DepthTraversalTypeAwareCrawler(
57 new TypeAwareVisitorAdapter(CrawlerDepthLimit.FILE, POST_ORDER) {
59 public void visitFile(Component file) {
60 CloseableIterator<BatchReport.Duplication> duplications = batchReportReader.readComponentDuplications(file.getReportAttributes().getRef());
62 while (duplications.hasNext()) {
63 loadDuplications(file, duplications.next());
69 }).visit(treeRootHolder.getRoot());
72 private void loadDuplications(Component file, BatchReport.Duplication duplication) {
73 TextBlock original = convert(duplication.getOriginPosition());
74 for (BatchReport.Duplicate duplicate : duplication.getDuplicateList()) {
75 if (duplicate.hasOtherFileRef()) {
76 duplicationRepository.addDuplication(file, original, treeRootHolder.getComponentByRef(duplicate.getOtherFileRef()), convert(duplicate.getRange()));
78 duplicationRepository.addDuplication(file, original, convert(duplicate.getRange()));
83 private static TextBlock convert(BatchReport.TextRange textRange) {
84 return new TextBlock(textRange.getStartLine(), textRange.getEndLine());