001/* 002 * SonarQube 003 * Copyright (C) 2009-2017 SonarSource SA 004 * mailto:info AT sonarsource DOT com 005 * 006 * This program 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 * This program 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 License 017 * along with this program; if not, write to the Free Software Foundation, 018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 019 */ 020package org.sonar.api.batch.sensor.cpd.internal; 021 022import java.util.ArrayList; 023import java.util.List; 024import org.sonar.api.CoreProperties; 025import org.sonar.api.batch.fs.InputFile; 026import org.sonar.api.batch.fs.TextRange; 027import org.sonar.api.batch.fs.internal.PathPattern; 028import org.sonar.api.batch.sensor.cpd.NewCpdTokens; 029import org.sonar.api.batch.sensor.internal.DefaultStorable; 030import org.sonar.api.batch.sensor.internal.SensorStorage; 031import org.sonar.api.config.Configuration; 032import org.sonar.duplications.internal.pmd.TokensLine; 033 034import static com.google.common.base.Preconditions.checkState; 035import static java.util.Collections.unmodifiableList; 036import static java.util.Objects.requireNonNull; 037 038public class DefaultCpdTokens extends DefaultStorable implements NewCpdTokens { 039 040 private final Configuration config; 041 private final ArrayList<TokensLine> result = new ArrayList<>(); 042 private InputFile inputFile; 043 private int startLine = Integer.MIN_VALUE; 044 private int startIndex = 0; 045 private int currentIndex = 0; 046 private StringBuilder sb = new StringBuilder(); 047 private TextRange lastRange; 048 private boolean excluded; 049 050 public DefaultCpdTokens(Configuration config, SensorStorage storage) { 051 super(storage); 052 this.config = config; 053 } 054 055 @Override 056 public DefaultCpdTokens onFile(InputFile inputFile) { 057 this.inputFile = requireNonNull(inputFile, "file can't be null"); 058 String[] cpdExclusions = config.getStringArray(CoreProperties.CPD_EXCLUSIONS); 059 for (PathPattern cpdExclusion : PathPattern.create(cpdExclusions)) { 060 if (cpdExclusion.match(inputFile.absolutePath(), inputFile.relativePath())) { 061 this.excluded = true; 062 } 063 } 064 return this; 065 } 066 067 public InputFile inputFile() { 068 return inputFile; 069 } 070 071 @Override 072 public NewCpdTokens addToken(int startLine, int startLineOffset, int endLine, int endLineOffset, String image) { 073 checkInputFileNotNull(); 074 TextRange newRange; 075 try { 076 newRange = inputFile.newRange(startLine, startLineOffset, endLine, endLineOffset); 077 } catch (Exception e) { 078 throw new IllegalArgumentException("Unable to register token in file " + inputFile, e); 079 } 080 return addToken(newRange, image); 081 } 082 083 @Override 084 public DefaultCpdTokens addToken(TextRange range, String image) { 085 requireNonNull(range, "Range should not be null"); 086 requireNonNull(image, "Image should not be null"); 087 checkInputFileNotNull(); 088 if (excluded) { 089 return this; 090 } 091 checkState(lastRange == null || lastRange.end().compareTo(range.start()) <= 0, 092 "Tokens of file %s should be provided in order.\nPrevious token: %s\nLast token: %s", inputFile, lastRange, range); 093 094 String value = image; 095 096 int line = range.start().line(); 097 if (line != startLine) { 098 addNewTokensLine(result, startIndex, currentIndex, startLine, sb); 099 startIndex = currentIndex + 1; 100 startLine = line; 101 } 102 currentIndex++; 103 sb.append(value); 104 lastRange = range; 105 106 return this; 107 } 108 109 public List<TokensLine> getTokenLines() { 110 return unmodifiableList(new ArrayList<>(result)); 111 } 112 113 private static void addNewTokensLine(List<TokensLine> result, int startUnit, int endUnit, int startLine, StringBuilder sb) { 114 if (sb.length() != 0) { 115 result.add(new TokensLine(startUnit, endUnit, startLine, sb.toString())); 116 sb.setLength(0); 117 } 118 } 119 120 @Override 121 protected void doSave() { 122 checkState(inputFile != null, "Call onFile() first"); 123 if (excluded) { 124 return; 125 } 126 addNewTokensLine(result, startIndex, currentIndex, startLine, sb); 127 storage.store(this); 128 } 129 130 private void checkInputFileNotNull() { 131 checkState(inputFile != null, "Call onFile() first"); 132 } 133}