org.sonar.cxx.sensors.rats.CxxRatsSensor.java Source code

Java tutorial

Introduction

Here is the source code for org.sonar.cxx.sensors.rats.CxxRatsSensor.java

Source

/*
 * Sonar C++ Plugin (Community)
 * Copyright (C) 2010-2018 SonarOpenCommunity
 * http://github.com/SonarOpenCommunity/sonar-cxx
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.cxx.sensors.rats;

import java.io.File;
import java.util.List;

import javax.annotation.Nullable;

import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.input.sax.XMLReaders;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.SensorDescriptor;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.cxx.CxxLanguage;
import org.sonar.cxx.CxxMetricsFactory;
import org.sonar.cxx.sensors.utils.CxxIssuesReportSensor;
import org.sonar.cxx.sensors.utils.CxxUtils;
import org.sonar.cxx.utils.CxxReportIssue;

/**
 * {@inheritDoc}
 */
public class CxxRatsSensor extends CxxIssuesReportSensor {

    private static final Logger LOG = Loggers.get(CxxRatsSensor.class);
    private static final String MISSING_RATS_TYPE = "fixed size global buffer";
    public static final String REPORT_PATH_KEY = "rats.reportPath";

    /**
     * CxxRatsSensor for RATS Sensor
     *
     * @param language defines settings C or C++
     */
    public CxxRatsSensor(CxxLanguage language) {
        super(language, REPORT_PATH_KEY, CxxRatsRuleRepository.getRepositoryKey(language));
    }

    @Override
    public void describe(SensorDescriptor descriptor) {
        descriptor.name(getLanguage().getName() + " RatsSensor").onlyOnLanguage(getLanguage().getKey())
                .createIssuesForRuleRepository(getRuleRepositoryKey())
                .onlyWhenConfiguration(conf -> conf.hasKey(getReportPathKey()));
    }

    @Override
    protected void processReport(final SensorContext context, File report)
            throws org.jdom2.JDOMException, java.io.IOException {
        LOG.debug("Parsing 'RATS' format");

        try {
            SAXBuilder builder = new SAXBuilder(XMLReaders.NONVALIDATING);
            Element root = builder.build(report).getRootElement();
            List<Element> vulnerabilities = root.getChildren("vulnerability");
            for (Element vulnerability : vulnerabilities) {
                String type = getVulnerabilityType(vulnerability.getChild("type"));
                String message = vulnerability.getChild("message").getTextTrim();

                List<Element> files = vulnerability.getChildren("file");

                for (Element file : files) {
                    String fileName = file.getChild("name").getTextTrim();

                    List<Element> lines = file.getChildren("line");
                    for (Element lineElem : lines) {
                        String line = lineElem.getTextTrim();

                        CxxReportIssue issue = new CxxReportIssue(type, fileName, line, message);
                        saveUniqueViolation(context, issue);
                    }
                }
            }
        } catch (org.jdom2.input.JDOMParseException e) {
            // when RATS fails the XML file might be incomplete
            LOG.error("Ignore incomplete XML output from RATS '{}'", CxxUtils.getStackTrace(e));
        }
    }

    private static String getVulnerabilityType(@Nullable Element child) {
        if (child != null) {
            return child.getTextTrim();
        }
        return MISSING_RATS_TYPE;
    }

    @Override
    protected CxxMetricsFactory.Key getMetricKey() {
        return CxxMetricsFactory.Key.RATS_SENSOR_ISSUES_KEY;
    }
}