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