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