org.sonar.plugins.plsqltoad.PlSqlToadResultsParser.java Source code

Java tutorial

Introduction

Here is the source code for org.sonar.plugins.plsqltoad.PlSqlToadResultsParser.java

Source

/*
 * Sonar PL/SQL Toad Plugin
 * Copyright (C) 2012 SonarSource
 * dev@sonar.codehaus.org
 *
 * 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  02
 */
package org.sonar.plugins.plsqltoad;

import com.google.common.base.Charsets;
import org.apache.commons.lang.math.NumberUtils;
import org.codehaus.stax2.XMLInputFactory2;
import org.codehaus.staxmate.SMInputFactory;
import org.codehaus.staxmate.in.SMEvent;
import org.codehaus.staxmate.in.SMHierarchicCursor;
import org.codehaus.staxmate.in.SMInputCursor;
import org.codehaus.staxmate.in.SimpleFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
import org.sonar.api.rules.ActiveRule;
import org.sonar.api.rules.Violation;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

public class PlSqlToadResultsParser {

    private static final Logger LOG = LoggerFactory.getLogger(PlSqlToadResultsParser.class);

    private final Project project;
    private final SensorContext context;
    private final RulesProfile profile;

    private String currentFileAbsolutePath;

    public PlSqlToadResultsParser(Project project, SensorContext context, RulesProfile profile) {
        this.project = project;
        this.context = context;
        this.profile = profile;
    }

    public void parse(PlSqlToadPage page) {
        parse(page.getReportFile());
    }

    private void parse(File codeXpertXml) {
        LOG.info("Parsing Toad's result XML file {}..", codeXpertXml.getAbsolutePath());

        currentFileAbsolutePath = null;

        try {
            XMLInputFactory xmlFactory = XMLInputFactory2.newInstance();
            InputStream is = new AsciiFilterInputStream(new FileInputStream(codeXpertXml));
            SMHierarchicCursor rootCursor = SMInputFactory
                    .rootElementCursor(xmlFactory.createXMLStreamReader(is, Charsets.US_ASCII.name()));

            rootCursor.advance();

            try {
                SMInputCursor objectCursor = rootCursor.childElementCursor("object");
                while (objectCursor.getNext() != null) {
                    processFile(objectCursor.descendantElementCursor());
                }
            } finally {
                rootCursor.getStreamReader().closeCompletely();
            }
        } catch (FileNotFoundException e) {
            throw new PlSqlToadException(
                    "Unable to find the Toad's result XML file at " + codeXpertXml.getAbsolutePath(), e);
        } catch (XMLStreamException e) {
            throw new PlSqlToadException("Error while parsing Toad's result XML file at "
                    + codeXpertXml.getAbsolutePath() + ", current source file = "
                    + (currentFileAbsolutePath == null ? "[unknown]" : currentFileAbsolutePath) + "\n"
                    + e.getMessage(), e);
        }
    }

    private void processFile(SMInputCursor cursor) throws XMLStreamException {
        cursor.setFilter(new SimpleFilter(SMEvent.START_ELEMENT));
        Resource resource = null;

        while (cursor.getNext() != null) {
            if ("object_name".equals(cursor.getLocalName())) {
                currentFileAbsolutePath = cursor.collectDescendantText();
                resource = org.sonar.api.resources.File.fromIOFile(new File(currentFileAbsolutePath), project);
            } else if ("rule".equals(cursor.getLocalName())) {
                processRule(resource, cursor);
            }
        }
    }

    private void processRule(Resource resource, SMInputCursor ruleCursor) throws XMLStreamException {
        String key = ruleCursor.getAttrValue("code");
        String description = ruleCursor.getAttrValue("description");

        ActiveRule activeRule = PlSqlToadRuleHelper.getActiveRule(profile, key);
        if (activeRule == null) {
            return;
        }

        SMInputCursor violationsCursor = ruleCursor.childElementCursor("rule_text");
        while (violationsCursor.getNext() != null) {
            int defaultLine = NumberUtils.toInt(violationsCursor.getAttrValue("line"));
            int line = NumberUtils.toInt(violationsCursor.getAttrValue("linenum"), defaultLine);

            Violation violation = Violation.create(activeRule, resource).setMessage(description);

            if (line >= 1) {
                violation.setLineId(line);
            }

            context.saveViolation(violation);
        }
    }

}