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.sonar.api.batch.fs.InputFile;
026import org.sonar.api.batch.sensor.Sensor;
027import org.sonar.api.batch.sensor.SensorContext;
028import org.sonar.api.batch.sensor.SensorDescriptor;
029import org.sonar.api.component.ResourcePerspectives;
030import org.sonar.api.source.Symbol;
031import org.sonar.api.source.Symbolizable;
032import org.sonar.api.utils.log.Logger;
033import org.sonar.api.utils.log.Loggers;
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 ResourcePerspectives perspectives;
047
048  public SymbolReferencesSensor(ResourcePerspectives perspectives) {
049    this.perspectives = perspectives;
050  }
051
052  private static final Logger LOG = Loggers.get(SymbolReferencesSensor.class);
053
054  private static final String SYMBOL_EXTENSION = ".symbol";
055
056  private void processFileSymbol(InputFile inputFile, SensorContext context) {
057    File ioFile = inputFile.file();
058    File symbolFile = new File(ioFile.getParentFile(), ioFile.getName() + SYMBOL_EXTENSION);
059    if (symbolFile.exists()) {
060      LOG.debug("Processing " + symbolFile.getAbsolutePath());
061      try {
062        List<String> lines = FileUtils.readLines(symbolFile, context.fileSystem().encoding().name());
063        int lineNumber = 0;
064        Symbolizable symbolizable = perspectives.as(Symbolizable.class, inputFile);
065        if (symbolizable != null) {
066          Symbolizable.SymbolTableBuilder symbolTableBuilder = symbolizable.newSymbolTableBuilder();
067          for (String line : lines) {
068            lineNumber++;
069            if (StringUtils.isBlank(line) || line.startsWith("#")) {
070              continue;
071            }
072            processLine(symbolFile, lineNumber, symbolTableBuilder, line);
073          }
074          symbolizable.setSymbolTable(symbolTableBuilder.build());
075        }
076      } catch (IOException e) {
077        throw new IllegalStateException(e);
078      }
079    }
080  }
081
082  private void processLine(File symbolFile, int lineNumber, Symbolizable.SymbolTableBuilder symbolTableBuilder, String line) {
083    try {
084      Iterator<String> split = Splitter.on(",").split(line).iterator();
085      int startOffset = Integer.parseInt(split.next());
086      int endOffset = Integer.parseInt(split.next());
087      Symbol s = symbolTableBuilder.newSymbol(startOffset, endOffset);
088      while (split.hasNext()) {
089        symbolTableBuilder.newReference(s, Integer.parseInt(split.next()));
090      }
091    } catch (Exception e) {
092      throw new IllegalStateException("Error processing line " + lineNumber + " of file " + symbolFile.getAbsolutePath(), e);
093    }
094  }
095
096  @Override
097  public void describe(SensorDescriptor descriptor) {
098    descriptor
099      .name("Xoo Symbol Reference Sensor")
100      .onlyOnLanguages(Xoo.KEY);
101  }
102
103  @Override
104  public void execute(SensorContext context) {
105    for (InputFile file : context.fileSystem().inputFiles(context.fileSystem().predicates().hasLanguages(Xoo.KEY))) {
106      processFileSymbol(file, context);
107    }
108  }
109}