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