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.scm;
021    
022    import com.google.common.annotations.VisibleForTesting;
023    import com.google.common.base.Charsets;
024    import org.apache.commons.io.FileUtils;
025    import org.apache.commons.lang.StringUtils;
026    import org.sonar.api.batch.fs.InputFile;
027    import org.sonar.api.batch.scm.BlameCommand;
028    import org.sonar.api.batch.scm.BlameLine;
029    import org.sonar.api.utils.DateUtils;
030    
031    import java.io.File;
032    import java.io.IOException;
033    import java.util.ArrayList;
034    import java.util.List;
035    
036    public class XooBlameCommand extends BlameCommand {
037    
038      private static final String SCM_EXTENSION = ".scm";
039    
040      @Override
041      public void blame(BlameInput input, BlameOutput result) {
042        for (InputFile inputFile : input.filesToBlame()) {
043          processFile(inputFile, result);
044        }
045      }
046    
047      @VisibleForTesting
048      protected void processFile(InputFile inputFile, BlameOutput result) {
049        File ioFile = inputFile.file();
050        File scmDataFile = new java.io.File(ioFile.getParentFile(), ioFile.getName() + SCM_EXTENSION);
051        if (!scmDataFile.exists()) {
052          return;
053        }
054    
055        try {
056          List<String> lines = FileUtils.readLines(scmDataFile, Charsets.UTF_8.name());
057          List<BlameLine> blame = new ArrayList<BlameLine>(lines.size());
058          int lineNumber = 0;
059          for (String line : lines) {
060            lineNumber++;
061            if (StringUtils.isNotBlank(line)) {
062              // revision,author,dateTime
063              String[] fields = StringUtils.splitPreserveAllTokens(line, ',');
064              if (fields.length < 3) {
065                throw new IllegalStateException("Not enough fields on line " + lineNumber);
066              }
067              String revision = StringUtils.trimToNull(fields[0]);
068              String author = StringUtils.trimToNull(fields[1]);
069              BlameLine blameLine = new BlameLine().revision(revision).author(author);
070              String dateStr = StringUtils.trimToNull(fields[2]);
071              // Will throw an exception, when date is not in format "yyyy-MM-dd"
072              if (dateStr != null) {
073                blameLine.date(DateUtils.parseDate(dateStr));
074              }
075              blame.add(blameLine);
076            }
077          }
078          result.blameResult(inputFile, blame);
079        } catch (IOException e) {
080          throw new IllegalStateException(e);
081        }
082      }
083    }