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 org.apache.commons.io.FileUtils;
024import org.apache.commons.lang.StringUtils;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
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.symbol.Symbol;
032import org.sonar.api.batch.sensor.symbol.SymbolTableBuilder;
033import org.sonar.api.measures.CoreMetrics;
034import org.sonar.xoo.Xoo;
035
036import java.io.File;
037import java.io.IOException;
038import java.util.Iterator;
039import java.util.List;
040
041/**
042 * Parse files *.xoo.symbol
043 */
044public class SymbolReferencesSensor implements Sensor {
045
046  private static final Logger LOG = LoggerFactory.getLogger(SymbolReferencesSensor.class);
047
048  private static final String SYMBOL_EXTENSION = ".symbol";
049
050  private void processFileSymbol(InputFile inputFile, SensorContext context) {
051    File ioFile = inputFile.file();
052    File symbolFile = new File(ioFile.getParentFile(), ioFile.getName() + SYMBOL_EXTENSION);
053    if (symbolFile.exists()) {
054      LOG.debug("Processing " + symbolFile.getAbsolutePath());
055      try {
056        List<String> lines = FileUtils.readLines(symbolFile, context.fileSystem().encoding().name());
057        int lineNumber = 0;
058        SymbolTableBuilder symbolTableBuilder = context.symbolTableBuilder(inputFile);
059        for (String line : lines) {
060          lineNumber++;
061          if (StringUtils.isBlank(line) || line.startsWith("#")) {
062            continue;
063          }
064          processLine(symbolFile, lineNumber, symbolTableBuilder, line);
065        }
066        symbolTableBuilder.done();
067      } catch (IOException e) {
068        throw new IllegalStateException(e);
069      }
070    }
071  }
072
073  private void processLine(File symbolFile, int lineNumber, SymbolTableBuilder symbolTableBuilder, String line) {
074    try {
075      Iterator<String> split = Splitter.on(",").split(line).iterator();
076      int startOffset = Integer.parseInt(split.next());
077      int endOffset = Integer.parseInt(split.next());
078      Symbol s = symbolTableBuilder.newSymbol(startOffset, endOffset);
079      while (split.hasNext()) {
080        symbolTableBuilder.newReference(s, Integer.parseInt(split.next()));
081      }
082    } catch (Exception e) {
083      throw new IllegalStateException("Error processing line " + lineNumber + " of file " + symbolFile.getAbsolutePath(), e);
084    }
085  }
086
087  @Override
088  public void describe(SensorDescriptor descriptor) {
089    descriptor
090      .name("Xoo Symbol Reference Sensor")
091      .provides(CoreMetrics.LINES)
092      .workOnLanguages(Xoo.KEY)
093      .workOnFileTypes(InputFile.Type.MAIN, InputFile.Type.TEST);
094  }
095
096  @Override
097  public void execute(SensorContext context) {
098    for (InputFile file : context.fileSystem().inputFiles(context.fileSystem().predicates().hasLanguages(Xoo.KEY))) {
099      processFileSymbol(file, context);
100    }
101  }
102}