001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube 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 * SonarQube 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.xoo.lang;
021
022import com.google.common.base.Splitter;
023import com.google.common.collect.Lists;
024import org.apache.commons.io.FileUtils;
025import org.sonar.api.CoreProperties;
026import org.sonar.api.batch.fs.FilePredicates;
027import org.sonar.api.batch.fs.InputFile;
028import org.sonar.api.batch.sensor.Sensor;
029import org.sonar.api.batch.sensor.SensorContext;
030import org.sonar.api.batch.sensor.SensorDescriptor;
031import org.sonar.api.batch.sensor.duplication.DuplicationTokenBuilder;
032import org.sonar.xoo.Xoo;
033
034import java.io.File;
035import java.io.IOException;
036import java.util.List;
037
038/**
039 * Tokenize xoo files (separator is whitespace) for duplication detection
040 */
041public class XooTokenizerSensor implements Sensor {
042
043  private void computeTokens(InputFile inputFile, SensorContext context) {
044    DuplicationTokenBuilder tokenBuilder = context.duplicationTokenBuilder(inputFile);
045    File ioFile = inputFile.file();
046    int lineId = 0;
047    try {
048      for (String line : FileUtils.readLines(ioFile)) {
049        lineId++;
050        for (String token : Splitter.on(" ").split(line)) {
051          tokenBuilder.addToken(lineId, token);
052        }
053      }
054      tokenBuilder.done();
055    } catch (IOException e) {
056      throw new IllegalStateException("Unable to read file " + ioFile, e);
057    }
058  }
059
060  @Override
061  public void describe(SensorDescriptor descriptor) {
062    descriptor
063      .name("Xoo Tokenizer Sensor")
064      .workOnLanguages(Xoo.KEY)
065      .workOnFileTypes(InputFile.Type.MAIN);
066  }
067
068  @Override
069  public void execute(SensorContext context) {
070    String[] cpdExclusions = context.settings().getStringArray(CoreProperties.CPD_EXCLUSIONS);
071    FilePredicates p = context.fileSystem().predicates();
072    List<InputFile> sourceFiles = Lists.newArrayList(context.fileSystem().inputFiles(p.and(
073      p.hasType(InputFile.Type.MAIN),
074      p.hasLanguage(Xoo.KEY),
075      p.doesNotMatchPathPatterns(cpdExclusions)
076      )));
077    if (sourceFiles.isEmpty()) {
078      return;
079    }
080    for (InputFile file : sourceFiles) {
081      computeTokens(file, context);
082    }
083  }
084}