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

org.sonar.plugins.csharp.gendarme.results.GendarmeResultParser Maven / Gradle / Ivy

/*
 * Sonar C# Plugin :: Gendarme
 * Copyright (C) 2010 Jose Chillan, Alexandre Victoor and 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.csharp.gendarme.results;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

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

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
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.sonar.api.BatchExtension;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleFinder;
import org.sonar.api.rules.RuleQuery;
import org.sonar.api.utils.SonarException;
import org.sonar.plugins.csharp.gendarme.GendarmeConstants;

/**
 * Parses the reports generated by a Gendarme analysis.
 */
public class GendarmeResultParser implements BatchExtension {

  private RuleFinder ruleFinder;
  private Charset encoding;
  private GendarmeViolationMaker gendarmeViolationMaker;

  /**
   * Constructs a @link{GendarmeResultParser}.
   * 
   * @param project
   * @param context
   * @param rulesManager
   * @param profile
   */
  public GendarmeResultParser(RuleFinder ruleFinder, GendarmeViolationMaker gendarmeViolationMaker) {
    super();
    this.ruleFinder = ruleFinder;
    this.gendarmeViolationMaker = gendarmeViolationMaker;
    this.encoding = Charset.defaultCharset();
  }

  /**
   * Parses a processed violation file.
   * 
   * @param file
   *          the file to parse
   */
  public void parse(File file) {
    SMInputFactory inputFactory = initStax();
    FileInputStream fileInputStream = null;
    try {
      fileInputStream = new FileInputStream(file);
      SMHierarchicCursor cursor = inputFactory.rootElementCursor(new InputStreamReader(fileInputStream, encoding));
      SMInputCursor rulesCursor = cursor.advance().descendantElementCursor("rule");
      parseRuleBlocs(rulesCursor);
      cursor.getStreamReader().closeCompletely();
    } catch (XMLStreamException e) {
      throw new SonarException("Error while reading Gendarme result file: " + file.getAbsolutePath(), e);
    } catch (FileNotFoundException e) {
      throw new SonarException("Cannot find Gendarme result file: " + file.getAbsolutePath(), e);
    } finally {
      IOUtils.closeQuietly(fileInputStream);
    }
  }

  private SMInputFactory initStax() {
    XMLInputFactory xmlFactory = XMLInputFactory2.newInstance();
    xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
    xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
    xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
    xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
    return new SMInputFactory(xmlFactory);
  }

  private void parseRuleBlocs(SMInputCursor cursor) throws XMLStreamException {
    RuleQuery ruleQuery = RuleQuery.create().withRepositoryKey(GendarmeConstants.REPOSITORY_KEY);
    // Cursor is on 
    while (cursor.getNext() != null) {
      if (cursor.getCurrEvent().equals(SMEvent.START_ELEMENT)) {
        Rule currentRule = ruleFinder.find(ruleQuery.withKey(cursor.getAttrValue("Name")));
        if (currentRule != null) {
          String type = cursor.getAttrValue("Type");
          if (StringUtils.isEmpty(type)) {
            parseRuleDefects(cursor, currentRule);
          } else {
            gendarmeViolationMaker.registerRuleType(currentRule, type);
          }
        }
      }
    }
  }

  private void parseRuleDefects(SMInputCursor cursor, Rule currentRule) throws XMLStreamException {
    gendarmeViolationMaker.setCurrentRule(currentRule);
    gendarmeViolationMaker.setCurrentDefaultViolationMessage("");
    SMInputCursor childCursor = cursor.childElementCursor();
    while (childCursor.getNext() != null) {
      if (childCursor.getQName().getLocalPart().equals("problem")) {
        gendarmeViolationMaker.setCurrentDefaultViolationMessage(childCursor.collectDescendantText().trim());
      } else if (childCursor.getQName().getLocalPart().equals("target")) {
        parseTargetBloc(childCursor);
      }
    }
  }

  private void parseTargetBloc(SMInputCursor cursor) throws XMLStreamException {
    // Cursor is on 
    gendarmeViolationMaker.setCurrentTargetName(cursor.getAttrValue("Name"));
    gendarmeViolationMaker.setCurrentTargetAssembly(cursor.getAttrValue("Assembly"));
    SMInputCursor defectCursor = cursor.childElementCursor();
    while (defectCursor.getNext() != null) {
      // Cursor is on 
      gendarmeViolationMaker.setCurrentLocation(defectCursor.getAttrValue("Location"));
      gendarmeViolationMaker.setCurrentSource(defectCursor.getAttrValue("Source"));
      gendarmeViolationMaker.setCurrentMessage(defectCursor.collectDescendantText().trim());
      gendarmeViolationMaker.createViolation();
    }
  }

  /**
   * Sets the encoding to use to parse the result file
   * 
   * @param encoding
   *          the encoding to set
   */
  public void setEncoding(Charset encoding) {
    this.encoding = encoding;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy