001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2013 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.api.utils;
021
022import com.google.common.collect.Maps;
023import org.apache.commons.io.FilenameUtils;
024import org.apache.commons.lang.StringUtils;
025import org.codehaus.staxmate.in.SMHierarchicCursor;
026import org.codehaus.staxmate.in.SMInputCursor;
027import org.sonar.api.batch.SensorContext;
028import org.sonar.api.measures.CoverageMeasuresBuilder;
029import org.sonar.api.measures.Measure;
030import org.sonar.api.resources.Resource;
031
032import javax.xml.stream.XMLStreamException;
033
034import java.io.File;
035import java.text.ParseException;
036import java.util.Map;
037
038import static java.util.Locale.ENGLISH;
039import static org.sonar.api.utils.ParsingUtils.parseNumber;
040
041/**
042 * @since 3.7
043 */
044public class CoberturaReportParserUtils {
045
046  private CoberturaReportParserUtils() {
047  }
048
049  public interface FileResolver {
050
051    /**
052     * Return a SonarQube file resource from a filename present in Cobertura report
053     */
054    Resource resolve(String filename);
055  }
056
057  /**
058   * Parse a Cobertura xml report and create measures accordingly
059   */
060  public static void parseReport(File xmlFile, final SensorContext context, final FileResolver fileResolver) {
061    try {
062      StaxParser parser = new StaxParser(new StaxParser.XmlStreamHandler() {
063
064        public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException {
065          rootCursor.advance();
066          collectPackageMeasures(rootCursor.descendantElementCursor("package"), context, fileResolver);
067        }
068      });
069      parser.parse(xmlFile);
070    } catch (XMLStreamException e) {
071      throw new XmlParserException(e);
072    }
073  }
074
075  private static void collectPackageMeasures(SMInputCursor pack, SensorContext context, final FileResolver fileResolver) throws XMLStreamException {
076    while (pack.getNext() != null) {
077      Map<String, CoverageMeasuresBuilder> builderByFilename = Maps.newHashMap();
078      collectFileMeasures(pack.descendantElementCursor("class"), builderByFilename);
079      for (Map.Entry<String, CoverageMeasuresBuilder> entry : builderByFilename.entrySet()) {
080        String filename = sanitizeFilename(entry.getKey());
081        Resource file = fileResolver.resolve(filename);
082        if (fileExists(context, file)) {
083          for (Measure measure : entry.getValue().createMeasures()) {
084            context.saveMeasure(file, measure);
085          }
086        }
087      }
088    }
089  }
090
091  private static boolean fileExists(SensorContext context, Resource file) {
092    return context.getResource(file) != null;
093  }
094
095  private static void collectFileMeasures(SMInputCursor clazz, Map<String, CoverageMeasuresBuilder> builderByFilename) throws XMLStreamException {
096    while (clazz.getNext() != null) {
097      String fileName = clazz.getAttrValue("filename");
098      CoverageMeasuresBuilder builder = builderByFilename.get(fileName);
099      if (builder == null) {
100        builder = CoverageMeasuresBuilder.create();
101        builderByFilename.put(fileName, builder);
102      }
103      collectFileData(clazz, builder);
104    }
105  }
106
107  private static void collectFileData(SMInputCursor clazz, CoverageMeasuresBuilder builder) throws XMLStreamException {
108    SMInputCursor line = clazz.childElementCursor("lines").advance().childElementCursor("line");
109    while (line.getNext() != null) {
110      int lineId = Integer.parseInt(line.getAttrValue("number"));
111      try {
112        builder.setHits(lineId, (int) parseNumber(line.getAttrValue("hits"), ENGLISH));
113      } catch (ParseException e) {
114        throw new XmlParserException(e);
115      }
116
117      String isBranch = line.getAttrValue("branch");
118      String text = line.getAttrValue("condition-coverage");
119      if (StringUtils.equals(isBranch, "true") && StringUtils.isNotBlank(text)) {
120        String[] conditions = StringUtils.split(StringUtils.substringBetween(text, "(", ")"), "/");
121        builder.setConditions(lineId, Integer.parseInt(conditions[1]), Integer.parseInt(conditions[0]));
122      }
123    }
124  }
125
126  private static String sanitizeFilename(String s) {
127    String fileName = FilenameUtils.removeExtension(s);
128    fileName = fileName.replace('/', '.').replace('\\', '.');
129    return fileName;
130  }
131
132}