All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.plugins.flex.flexpmd.FlexPmdXmlReportParser Maven / Gradle / Ivy

/*
 * Sonar Flex Plugin
 * Copyright (C) 2010 SonarSource
 * [email protected]
 *
 * 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.flex.flexpmd;

import java.io.File;

import javax.xml.stream.XMLStreamException;

import org.apache.commons.lang.StringUtils;
import org.codehaus.staxmate.in.SMHierarchicCursor;
import org.codehaus.staxmate.in.SMInputCursor;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.resources.Project;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleFinder;
import org.sonar.api.rules.RuleQuery;
import org.sonar.api.rules.Violation;
import org.sonar.api.utils.StaxParser;
import org.sonar.plugins.flex.core.FlexUtils;

public class FlexPmdXmlReportParser {

  private Project project;
  private SensorContext context;
  private RuleFinder ruleFinder;

  public FlexPmdXmlReportParser(Project project, SensorContext context, RuleFinder ruleFinder) {
    this.project = project;
    this.context = context;
    this.ruleFinder = ruleFinder;
  }

  public void parse(File xmlFile) {
    StaxParser parser = new StaxParser(new StaxParser.XmlStreamHandler() {
      public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException {
        rootCursor.advance();

        SMInputCursor fileCursor = rootCursor.descendantElementCursor("file");
        while (fileCursor.getNext() != null) {
          String fileName = fileCursor.getAttrValue("name");

          SMInputCursor violationCursor = fileCursor.descendantElementCursor("violation");
          while (violationCursor.getNext() != null) {
            String beginLine = violationCursor.getAttrValue("beginline");
            String ruleKey = "com." + violationCursor.getAttrValue("rule") + "Rule";
            String message = StringUtils.trim(violationCursor.collectDescendantText());

            log(context, ruleKey, fileName, Integer.parseInt(beginLine), message);
          }
        }
      }
    });

    try {
      parser.parse(xmlFile);
    } catch (XMLStreamException e) {
      FlexUtils.LOG.error("Error parsing file : {}", xmlFile);
    }
  }

  void log(SensorContext context, String ruleKey, String fileName, Integer line, String message) {
    RuleQuery ruleQuery = RuleQuery.create()
        .withRepositoryKey(FlexPmdConstants.REPOSITORY_KEY)
        .withConfigKey(ruleKey);
    Rule rule = ruleFinder.find(ruleQuery);
    if (rule != null) {
      org.sonar.api.resources.File resource = org.sonar.api.resources.File.fromIOFile(new File(fileName), project);
      Violation violation = Violation.create(rule, resource).setLineId(line).setMessage(message);
      context.saveViolation(violation);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy