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

org.sonar.java.xml.XmlAnalyzer Maven / Gradle / Ivy

The newest version!
/*
 * SonarQube Java
 * Copyright (C) 2012-2016 SonarSource SA
 * mailto:contact AT sonarsource DOT com
 *
 * 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.java.xml;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.java.SonarComponents;
import org.sonar.java.xml.maven.PomCheck;
import org.sonar.java.xml.maven.PomCheckContext;
import org.sonar.java.xml.maven.PomCheckContextImpl;
import org.sonar.java.xml.maven.PomParser;
import org.sonar.maven.model.maven2.MavenProject;
import org.sonar.squidbridge.ProgressReport;
import org.sonar.squidbridge.api.CodeVisitor;
import org.w3c.dom.Document;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;

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

public class XmlAnalyzer {

  private static final Logger LOG = LoggerFactory.getLogger(XmlAnalyzer.class);
  private final SonarComponents sonarComponents;
  private final List xmlChecks;
  private final List pomChecks;
  private final XPath xPath;

  public XmlAnalyzer(SonarComponents sonarComponents, CodeVisitor... visitors) {
    ImmutableList.Builder xmlChecksBuilder = ImmutableList.builder();
    ImmutableList.Builder pomChecksBuilder = ImmutableList.builder();
    for (CodeVisitor visitor : visitors) {
      if (visitor instanceof XmlCheck) {
        xmlChecksBuilder.add((XmlCheck) visitor);
      } else if (visitor instanceof PomCheck) {
        pomChecksBuilder.add((PomCheck) visitor);
      }
    }
    this.xmlChecks = xmlChecksBuilder.build();
    this.pomChecks = pomChecksBuilder.build();
    this.sonarComponents = sonarComponents;
    this.xPath = XPathFactory.newInstance().newXPath();
  }

  public void scan(Iterable files) {
    boolean hasChecks = !xmlChecks.isEmpty() || !pomChecks.isEmpty();
    if (hasChecks && Iterables.isEmpty(files)) {
      LOG.warn("No 'xml' file have been indexed.");
      return;
    }

    ProgressReport progressReport = new ProgressReport("Report about progress of Xml analyzer", TimeUnit.SECONDS.toMillis(10));
    progressReport.start(Lists.newArrayList(files));

    boolean successfulyCompleted = false;
    try {
      for (File file : files) {
        simpleScan(file);
        progressReport.nextFile();
      }
      successfulyCompleted = true;
    } finally {
      if (successfulyCompleted) {
        progressReport.stop();
      } else {
        progressReport.cancel();
      }
    }
  }

  private void simpleScan(File file) {
    Document document = XmlParser.parseXML(file);
    if (document != null) {
      simpleScanAsXmlFile(file, document);
      if ("pom.xml".equals(file.getName())) {
        simpleScanAsPomFile(file, document);
      }
    }
  }

  private void simpleScanAsXmlFile(File file, Document document) {
    XmlCheckContext context = new XmlCheckContextImpl(document, file, xPath, sonarComponents);
    for (XmlCheck check : xmlChecks) {
      check.scanFile(context);
    }
  }

  private void simpleScanAsPomFile(File file, Document document) {
    MavenProject project = PomParser.parseXML(file);
    if (project != null) {
      PomCheckContext context = new PomCheckContextImpl(project, document, file, xPath, sonarComponents);
      for (PomCheck check : pomChecks) {
        check.scanFile(context);
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy