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