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.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 * @deprecated in 4.2. This class should be handled internally by plugins
044 */
045@Deprecated
046public class CoberturaReportParserUtils {
047
048  private CoberturaReportParserUtils() {
049  }
050
051  public interface FileResolver {
052
053    /**
054     * Return a SonarQube file resource from a filename present in Cobertura report
055     */
056    Resource resolve(String filename);
057  }
058
059  /**
060   * Parse a Cobertura xml report and create measures accordingly
061   */
062  public static void parseReport(File xmlFile, final SensorContext context, final FileResolver fileResolver) {
063    try {
064      StaxParser parser = new StaxParser(new StaxParser.XmlStreamHandler() {
065
066        public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException {
067          rootCursor.advance();
068          collectPackageMeasures(rootCursor.descendantElementCursor("package"), context, fileResolver);
069        }
070      });
071      parser.parse(xmlFile);
072    } catch (XMLStreamException e) {
073      throw new XmlParserException(e);
074    }
075  }
076
077  private static void collectPackageMeasures(SMInputCursor pack, SensorContext context, final FileResolver fileResolver) throws XMLStreamException {
078    while (pack.getNext() != null) {
079      Map<String, CoverageMeasuresBuilder> builderByFilename = Maps.newHashMap();
080      collectFileMeasures(pack.descendantElementCursor("class"), builderByFilename);
081      for (Map.Entry<String, CoverageMeasuresBuilder> entry : builderByFilename.entrySet()) {
082        String filename = sanitizeFilename(entry.getKey());
083        Resource file = fileResolver.resolve(filename);
084        if (fileExists(context, file)) {
085          for (Measure measure : entry.getValue().createMeasures()) {
086            context.saveMeasure(file, measure);
087          }
088        }
089      }
090    }
091  }
092
093  private static boolean fileExists(SensorContext context, Resource file) {
094    return context.getResource(file) != null;
095  }
096
097  private static void collectFileMeasures(SMInputCursor clazz, Map<String, CoverageMeasuresBuilder> builderByFilename) throws XMLStreamException {
098    while (clazz.getNext() != null) {
099      String fileName = clazz.getAttrValue("filename");
100      CoverageMeasuresBuilder builder = builderByFilename.get(fileName);
101      if (builder == null) {
102        builder = CoverageMeasuresBuilder.create();
103        builderByFilename.put(fileName, builder);
104      }
105      collectFileData(clazz, builder);
106    }
107  }
108
109  private static void collectFileData(SMInputCursor clazz, CoverageMeasuresBuilder builder) throws XMLStreamException {
110    SMInputCursor line = clazz.childElementCursor("lines").advance().childElementCursor("line");
111    while (line.getNext() != null) {
112      int lineId = Integer.parseInt(line.getAttrValue("number"));
113      try {
114        builder.setHits(lineId, (int) parseNumber(line.getAttrValue("hits"), ENGLISH));
115      } catch (ParseException e) {
116        throw new XmlParserException(e);
117      }
118
119      String isBranch = line.getAttrValue("branch");
120      String text = line.getAttrValue("condition-coverage");
121      if (StringUtils.equals(isBranch, "true") && StringUtils.isNotBlank(text)) {
122        String[] conditions = StringUtils.split(StringUtils.substringBetween(text, "(", ")"), "/");
123        builder.setConditions(lineId, Integer.parseInt(conditions[1]), Integer.parseInt(conditions[0]));
124      }
125    }
126  }
127
128  private static String sanitizeFilename(String s) {
129    String fileName = FilenameUtils.removeExtension(s);
130    fileName = fileName.replace('/', '.').replace('\\', '.');
131    return fileName;
132  }
133
134}