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.highlighting.HighlightingBuilder;
032 import org.sonar.api.batch.sensor.highlighting.TypeOfText;
033 import org.sonar.api.measures.CoreMetrics;
034 import org.sonar.xoo.Xoo;
035
036 import java.io.File;
037 import java.io.IOException;
038 import java.util.Iterator;
039 import java.util.List;
040
041 /**
042 * Parse files *.xoo.highlighting
043 */
044 public class SyntaxHighlightingSensor implements Sensor {
045
046 private static final Logger LOG = LoggerFactory.getLogger(SyntaxHighlightingSensor.class);
047
048 private static final String HIGHLIGHTING_EXTENSION = ".highlighting";
049
050 private void processFileHighlighting(InputFile inputFile, SensorContext context) {
051 File ioFile = inputFile.file();
052 File highlightingFile = new File(ioFile.getParentFile(), ioFile.getName() + HIGHLIGHTING_EXTENSION);
053 if (highlightingFile.exists()) {
054 LOG.debug("Processing " + highlightingFile.getAbsolutePath());
055 try {
056 List<String> lines = FileUtils.readLines(highlightingFile, context.fileSystem().encoding().name());
057 int lineNumber = 0;
058 HighlightingBuilder highlightingBuilder = context.highlightingBuilder(inputFile);
059 for (String line : lines) {
060 lineNumber++;
061 if (StringUtils.isBlank(line) || line.startsWith("#")) {
062 continue;
063 }
064 processLine(highlightingFile, lineNumber, highlightingBuilder, line);
065 }
066 highlightingBuilder.done();
067 } catch (IOException e) {
068 throw new IllegalStateException(e);
069 }
070 }
071 }
072
073 private void processLine(File highlightingFile, int lineNumber, HighlightingBuilder highlightingBuilder, 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 TypeOfText type = TypeOfText.forCssClass(split.next());
079 highlightingBuilder.highlight(startOffset, endOffset, type);
080 } catch (Exception e) {
081 throw new IllegalStateException("Error processing line " + lineNumber + " of file " + highlightingFile.getAbsolutePath(), e);
082 }
083 }
084
085 @Override
086 public void describe(SensorDescriptor descriptor) {
087 descriptor
088 .name("Xoo Highlighting Sensor")
089 .provides(CoreMetrics.LINES)
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 processFileHighlighting(file, context);
098 }
099 }
100 }