001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.plugins.cpd;
021
022 import net.sourceforge.pmd.cpd.TokenEntry;
023 import org.slf4j.Logger;
024 import org.slf4j.LoggerFactory;
025 import org.sonar.api.batch.CpdMapping;
026 import org.sonar.api.batch.SensorContext;
027 import org.sonar.api.resources.Project;
028 import org.sonar.api.resources.Resource;
029 import org.sonar.duplications.cpd.Match;
030
031 import java.io.File;
032 import java.util.HashMap;
033 import java.util.Iterator;
034 import java.util.Map;
035
036 public class CpdAnalyser {
037
038 private static final Logger LOG = LoggerFactory.getLogger(CpdAnalyser.class);
039
040 private CpdMapping mapping;
041 private SensorContext context;
042 private Project project;
043
044 public CpdAnalyser(Project project, SensorContext context, CpdMapping mapping) {
045 this.mapping = mapping;
046 this.context = context;
047 this.project = project;
048 }
049
050 public void analyse(Iterator<Match> matches) {
051 Map<Resource, DuplicationsData> duplicationsData = new HashMap<Resource, DuplicationsData>();
052 while (matches.hasNext()) {
053 Match match = matches.next();
054
055 for (TokenEntry firstMark : match.getMarkSet()) {
056 String firstAbsolutePath = firstMark.getTokenSrcID();
057 int firstLine = firstMark.getBeginLine();
058
059 Resource firstFile = mapping.createResource(new File(firstAbsolutePath), project.getFileSystem().getSourceDirs());
060 if (firstFile == null) {
061 LOG.warn("CPD - File not found : {}", firstAbsolutePath);
062 continue;
063 }
064
065 DuplicationsData firstFileData = getDuplicationsData(duplicationsData, firstFile);
066 firstFileData.incrementDuplicatedBlock();
067
068 for (TokenEntry tokenEntry : match.getMarkSet()) {
069 String secondAbsolutePath = tokenEntry.getTokenSrcID();
070 int secondLine = tokenEntry.getBeginLine();
071 if (secondAbsolutePath.equals(firstAbsolutePath) && firstLine == secondLine) {
072 continue;
073 }
074 Resource secondFile = mapping.createResource(new File(secondAbsolutePath), project.getFileSystem().getSourceDirs());
075 if (secondFile == null) {
076 LOG.warn("CPD - File not found : {}", secondAbsolutePath);
077 continue;
078 }
079
080 String resourceKey = SonarEngine.getFullKey(project, secondFile);
081 firstFileData.cumulate(resourceKey, secondLine, firstLine, match.getLineCount());
082 }
083 }
084 }
085
086 for (Map.Entry<Resource, DuplicationsData> entry : duplicationsData.entrySet()) {
087 entry.getValue().save(context, entry.getKey());
088 }
089 }
090
091 private DuplicationsData getDuplicationsData(Map<Resource, DuplicationsData> fileContainer, Resource file) {
092 DuplicationsData data = fileContainer.get(file);
093 if (data == null) {
094 String resourceKey = SonarEngine.getFullKey(project, file);
095 data = new DuplicationsData(resourceKey);
096 fileContainer.put(file, data);
097 }
098 return data;
099 }
100
101 }