001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2011 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 java.io.File; 023 import java.util.HashMap; 024 import java.util.Iterator; 025 import java.util.Map; 026 027 import net.sourceforge.pmd.cpd.TokenEntry; 028 029 import org.slf4j.Logger; 030 import org.slf4j.LoggerFactory; 031 import org.sonar.api.batch.CpdMapping; 032 import org.sonar.api.batch.SensorContext; 033 import org.sonar.api.resources.Project; 034 import org.sonar.api.resources.Resource; 035 import org.sonar.duplications.cpd.Match; 036 037 public class CpdAnalyser { 038 039 private static final Logger LOG = LoggerFactory.getLogger(CpdAnalyser.class); 040 041 private CpdMapping mapping; 042 private SensorContext context; 043 private Project project; 044 045 public CpdAnalyser(Project project, SensorContext context, CpdMapping mapping) { 046 this.mapping = mapping; 047 this.context = context; 048 this.project = project; 049 } 050 051 public void analyse(Iterator<Match> matches) { 052 Map<Resource, DuplicationsData> duplicationsData = new HashMap<Resource, DuplicationsData>(); 053 while (matches.hasNext()) { 054 Match match = matches.next(); 055 056 for (TokenEntry firstMark : match.getMarkSet()) { 057 String firstAbsolutePath = firstMark.getTokenSrcID(); 058 int firstLine = firstMark.getBeginLine(); 059 060 Resource firstFile = mapping.createResource(new File(firstAbsolutePath), project.getFileSystem().getSourceDirs()); 061 if (firstFile == null) { 062 LOG.warn("CPD - File not found : {}", firstAbsolutePath); 063 continue; 064 } 065 066 DuplicationsData firstFileData = getDuplicationsData(duplicationsData, firstFile); 067 firstFileData.incrementDuplicatedBlock(); 068 069 for (TokenEntry tokenEntry : match.getMarkSet()) { 070 String secondAbsolutePath = tokenEntry.getTokenSrcID(); 071 int secondLine = tokenEntry.getBeginLine(); 072 if (secondAbsolutePath.equals(firstAbsolutePath) && firstLine == secondLine) { 073 continue; 074 } 075 Resource secondFile = mapping.createResource(new File(secondAbsolutePath), project.getFileSystem().getSourceDirs()); 076 if (secondFile == null) { 077 LOG.warn("CPD - File not found : {}", secondAbsolutePath); 078 continue; 079 } 080 081 firstFileData.cumulate(secondFile, secondLine, firstLine, match.getLineCount()); 082 } 083 } 084 } 085 086 for (DuplicationsData data : duplicationsData.values()) { 087 data.save(); 088 } 089 } 090 091 private DuplicationsData getDuplicationsData(Map<Resource, DuplicationsData> fileContainer, Resource file) { 092 DuplicationsData data = fileContainer.get(file); 093 if (data == null) { 094 data = new DuplicationsData(file, context); 095 fileContainer.put(file, data); 096 } 097 return data; 098 } 099 100 }